プログラム初心者です。
やりたいこと
text に記載された URL に 1 つずつアクセスし、情報を取得後 CSV 出力を行いたい。
困っていること
http.client.BadStatusLine
でエラーになってしまい出力できない。
エラーの修正方法も知りたいのですが、
それよりもこのようになった場合、何をどう調べて解決したら良いか方法がわかりません。
どのように調査し、解決へ導くことができるかを教えていただきたいです・・・。
発生している問題・エラーメッセージ
C:\Python>hogehoge.py Traceback (most recent call last): File "C:\Python\hogehoge.py", line 48, in <module> with urllib.request.urlopen(request) as res: File "C:\Users\hogehoge\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\Users\hogehoge\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 525, in open response = self._open(req, data) File "C:\Users\hogehoge\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 543, in _open '_open', req) File "C:\Users\hogehoge\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain result = func(*args) File "C:\Users\hogehoge\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 1360, in https_open context=self._context, check_hostname=self._check_hostname) File "C:\Users\hogehoge\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 1320, in do_open r = h.getresponse() File "C:\Users\hogehoge\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1321, in getresponse response.begin() File "C:\Users\hogehoge\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 296, in begin version, status, reason = self._read_status() File "C:\Users\hogehoge\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 278, in _read_status raise BadStatusLine(line) http.client.BadStatusLine: <html>
該当のソースコード
Python
1import requests 2from bs4 import BeautifulSoup 3import csv 4import urllib.request 5 6# ファイルをオープンする 7test_data = open("date_URL.txt", "r") 8 9# 行ごとにすべて読み込んでリストデータにする 10lines = test_data.readlines() 11 12# CSVファイルを開く。ファイルがなければ新規作成する。 13f = open("deta.csv", "w", encoding='utf-8') 14writecsv = csv.writer(f, lineterminator='\n') 15 16# 一行ずつ取得する 17for line in lines: 18 19 csvlist = [] 20 21~~~~~~~ 22~~~略~~~ 23~~~~~~~ 24 25 elif 'hogehoge.com' in line: 26 27 headers = { 28 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0", 29 } 30 31 url = line + "?Country=FR" 32 33 request = urllib.request.Request(url=url, headers=headers) 34 response = urllib.request.urlopen(request) 35 html = response.read().decode('utf-8') 36 soup = BeautifulSoup(html, "html.parser") 37 38 #品番 39 modelhtml = soup.find("div", class_="b-product_master_id") 40 modeltext = modelhtml.text 41 model = (modeltext.replace('Code: ', '').replace('\n', '')) 42 43 #価格 44 pricehtml = soup.find("h2", attrs={"class", "b-product_container-price js-product_container-price"}) 45 pricetext = pricehtml.find("h4").text 46 price = (pricetext.replace('€', '').replace('\n', '')) 47 48~~~~~~~ 49~~~略~~~ 50~~~~~~~
試したこと・調べたこと
1.
text 内に URL が 1 つの場合は正常終了
text 内に URL が 2 つ以上の場合は異常終了(エラー)
※text 内は改行して 1 行に 1URL で記載しています。
2.
https://docs.python.org/ja/3/library/http.client.html
exception http.client.BadStatusLine
HTTPException のサブクラスです。サーバが理解できない HTTP 状態コードで応答した場合に送出されます。
ここで躓いているのはわかっているのですが
なんで URL1 つの場合は成功して、URL2 つ以上の場合が失敗するのかが理解できていません・・・
備考
▼UserAgent を使用している理由
text に記載している URL へアクセスした時、Web スクレイピングができなかった為
こちらを参考に UserAgent を偽装しています。
https://teratail.com/questions/156800
バージョン
Python 3.7.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。