[やりたいこと]
HTTPヘッダーからエンコーディングを取得したい
python3
1 2import sys 3from urllib.request import urlopen 4 5f = urlopen('https://gihyo.jp/dp') 6 7headers = { 8"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0", 9} 10 11encoding = f.info().get_content_charset(failobj="utf-8") 12print('encoding:', encoding, file=sys.stderr) 13text = f.read().decode(encoding) 14print(text) 15
エラー内容
Traceback (most recent call last):
File "greet.py", line 4, in <module>
f = urlopen('https://gihyo.jp/dp')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 531, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
やったこと
参考にしたサイト
以下の部分を付け加えました。しかし同じエラーが出ました。
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0",
}
あなたの回答
tips
プレビュー