電子書籍の厳選無料作品が豊富!

一通り調べていじってみたのですがわからなかったので質問いたします。
プログラミング初学者です。
やりたいことは
・求人サイトで特定の地域のある職種の求人を毎日拾ってきてCSVでデスクトップへ保存する です。

以下chat gptに出力してもらったのに手を加えたものです。

import os
from datetime import datetime
import requests
from bs4 import BeautifulSoup
import csv

# 保存先のディレクトリを設定.
save_dir = os.path.join(os.path.expanduser("~"), "Desktop")

# 現在の日時を取得.
now = datetime.now()
date_string = now.strftime("%Y-%m-%d")

# CSVファイルの名前を作成.
csv_filename = os.path.join(save_dir, f"地域_職種_jobs_{date_string}.csv")

# ○○○○のURL.
URL = "https://jp.○○○○/jobs?q=検索条件"

# リクエストを送信.
response = requests.get(URL)
soup = BeautifulSoup(response.text, 'html.parser')

# 求人情報のリストを取得.
job_list = soup.find_all('div', class_='jobsearch-SerpJobCard')

# CSVファイルに書き込み.
with open(csv_filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(["Job Title", "Company", "Location", "Summary"])

for job in job_list:
job_title = job.find('h2', class_='title').text.strip()
company = job.find('span', class_='company').text.strip()
location = job.find('div', class_='location').text.strip()
summary = job.find('div', class_='summary').text.strip()

writer.writerow([job_title, company, location, summary])

print(f"求人情報をCSVファイルに保存しました: {csv_filename}")


以下 実行結果

import os
>>> from datetime import datetime
>>> import requests
>>> from bs4 import BeautifulSoup
>>> import csv
>>>
>>> # 保存先のディレクトリを設定.
>>> save_dir = os.path.join(os.path.expanduser("~"), "Desktop")
>>>
>>> # 現在の日時を取得.
>>> now = datetime.now()
>>> date_string = now.strftime("%Y-%m-%d")
>>>
>>> # CSVファイルの名前を作成.
>>> csv_filename = os.path.join(save_dir, f"atsugi_medical_jobs_{date_string}.csv")
>>>
>>> # ○○○○のURL.
>>> URL = "https://○○○○/jobs?q=検索条件" ←特定回避のため隠しています。
>>>
>>> # リクエストを送信.
>>> response = requests.get(URL)
>>> soup = BeautifulSoup(response.text, 'html.parser')
>>>
>>> # 求人情報のリストを取得.
>>> job_list = soup.find_all('div', class_='jobsearch-SerpJobCard')
>>>
>>> # CSVファイルに書き込み.
>>> with open(csv_filename, mode='w', newline='', encoding='utf-8') as file:
... writer = csv.writer(file)
... writer.writerow(["Job Title", "Company", "Location", "Summary"])
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\私のユーザー名\\Desktop\\地域_職種_jobs_2024-07-11.csv'
>>> for job in job_list:
File "<stdin>", line 1
for job in job_list:
IndentationError: unexpected indent
>>> job_title = job.find('h2', class_='title').text.strip()
File "<stdin>", line 1
job_title = job.find('h2', class_='title').text.strip()
IndentationError: unexpected indent
>>> company = job.find('span', class_='company').text.strip()
File "<stdin>", line 1
company = job.find('span', class_='company').text.strip()
IndentationError: unexpected indent
>>> location = job.find('div', class_='location').text.strip()
File "<stdin>", line 1
location = job.find('div', class_='location').text.strip()
IndentationError: unexpected indent
>>> summary = job.find('div', class_='summary').text.strip()
File "<stdin>", line 1
summary = job.find('div', class_='summary').text.strip()
IndentationError: unexpected indent
>>>
>>> writer.writerow([job_title, company, location, summary])
File "<stdin>", line 1
writer.writerow([job_title, company, location, summary])
IndentationError: unexpected indent
>>>
>>> print(f"求人情報をCSVファイルに保存しました: {csv_filename}")

となってしまいました。
CSVファイルの作成がそもそもできていないのでしょうか?
インデントはいろいろ調べていじってみたのですがどこを変えたらいいかわかりませんでした。

長文を読んでくださりありがとうございます。ぜひご回答お願いいたします。

  • 画像を添付する (ファイルサイズ:10MB以内、ファイル形式:JPG/GIF/PNG)
  • 今の自分の気分スタンプを選ぼう!
あと4000文字

A 回答 (2件)

save_dir = os.path.join(os.path.expanduser("~"), "Desktop")



save_dir = os.path.join(os.path.expanduser("~"), "OneDrive\デスクトップ")
に変えてみてください。

エラーは
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\私のユーザー名\\Desktop\\地域_職種_jobs_2024-07-11.csv'
で「そんなファイル or ディレクトリが無い」と言っています。

CSVファイルを作る前の段階でこのエラーが出ているということは、”そのディレクトリ(フォルダ)が無い、アクセスできない”ということです。
ここでいうディレクトリは「C:\\Users\\私のユーザー名\\Desktop\\」です。
Windows11からデスクトップフォルダのパスが変わった?っぽいので、そのパスが無いんでしょう。それが原因かと。

OneDriveは嫌だというのであれば、適当なパスにディレクトリを作ってsave_dirでそこを指すようにすればいいです。
    • good
    • 2
この回答へのお礼

ご回答ありがとうございます。
OneDrive追加したらいけました。

お礼日時:2024/07/12 10:19

「初学者」とお書きの程度が分かりませんが、やりたいことに書いてあることは初学者が出来る範囲を超えていそうです。



ChatGPTを使っても別に良いのですが、それはChatGPTから提示されたプログラムの意味を完全に自分が理解できる場合の話です。
自分の意図が正しく伝わったのか、それは期待通りの出力を出すプログラムなのかなどの判断が出来ない状態で、プログラムの各行の意味を分からないままで、あれこれいじるのは単なる時間の無駄だと思います。

そもそも、希望するデータが requestsで取れるのかどうかから不明です。
response = requests.get(URL)
の直後に、
print(response.text)
を書いて(以降のプログラム行を削除)、表示されたHTMLの中に希望データが含まれているかどうかを確認するのが第一歩でしょうか。
(HTMLが読めないとこのようなウェブページからの情報取得する分野は無理なので、読めないならまずHTMLを読めるようになる必要があります)
    • good
    • 2
この回答へのお礼

ご回答ありがとうございます。
いわれた通り文言追加&削除したところなにやら出てきましたが全く読めないのでHTMLも勉強してみようと思います。

お礼日時:2024/07/12 10:21

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!