python urllib.request - headers 可能有效
python urllib.request - headers that are likely to work
正在编写一个从网站获取信息的小脚本。我遇到了 HTTP 错误问题。
req = urllib.request.Request(lnk['href'],
headers={'User-Agent': 'Mozilla/5.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'})
page = urllib.request.urlopen(req)
例如,当尝试获取时,http://www.guru99.com/node-js-tutorial.html
我收到一长串错误,以 406 不可接受结尾:
Traceback (most recent call last):
File "get_links.py", line 45, in <module>
page = urllib.request.urlopen(req)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 471, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 581, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 509, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 443, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 589, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 406: Not Acceptable
谷歌搜索我发现我应该修复 headers(正如我上面所做的那样)和许多关于如何修复 headers 的教程。除了-实际上没有多少作用。
是否有一些好的 headers 可能不会对大多数网站造成问题?是否有其他人创建的某些 python 模块已经包含 commonly-working headers?有没有什么好的方法可以用不同的 headers 重试几次,直到得到好的回应?
这似乎是每个使用 Python 进行网络抓取的人都会遇到的问题,但我还没有找到合适的解决方案。
以下一组 headers 似乎适用于大多数测试。如果其他人有建议,请提供。如果一组不起作用,我也对尝试不同 headers 的好的解决方案感兴趣。
req = urllib.request.Request(lnk['href'],
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
page = urllib.request.urlopen(req)
我试过你的代码,我得到了与预期相同的错误。
我也用我的 Chrome-Browser 提供的 User-Agent 试过了,这似乎有效
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
.. 还有 运行 没有通过显式 header 的测试也返回了 http 200(成功)。这将使用库提供的默认 header,例如
python-requests/2.10.0
希望对您有所帮助
HTTP 错误 406 不可接受
The HyperText Transfer Protocol (HTTP) 406 Not Acceptable client error
response code indicates that the server cannot produce a response
matching the list of acceptable values defined in the request's
proactive content negotiation headers, and that the server is
unwilling to supply a default representation.
所以我可以看出问题出在您的 User-Agent: Mozilla/5.0
键和值上。这是一堆正确的用户代理的链接,
所以将您的代码更改为以下内容,
headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'})
我知道答案为时已晚,但希望这对其他人有所帮助。
正在编写一个从网站获取信息的小脚本。我遇到了 HTTP 错误问题。
req = urllib.request.Request(lnk['href'],
headers={'User-Agent': 'Mozilla/5.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'})
page = urllib.request.urlopen(req)
例如,当尝试获取时,http://www.guru99.com/node-js-tutorial.html
我收到一长串错误,以 406 不可接受结尾:
Traceback (most recent call last):
File "get_links.py", line 45, in <module>
page = urllib.request.urlopen(req)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 471, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 581, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 509, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 443, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 589, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 406: Not Acceptable
谷歌搜索我发现我应该修复 headers(正如我上面所做的那样)和许多关于如何修复 headers 的教程。除了-实际上没有多少作用。
是否有一些好的 headers 可能不会对大多数网站造成问题?是否有其他人创建的某些 python 模块已经包含 commonly-working headers?有没有什么好的方法可以用不同的 headers 重试几次,直到得到好的回应?
这似乎是每个使用 Python 进行网络抓取的人都会遇到的问题,但我还没有找到合适的解决方案。
以下一组 headers 似乎适用于大多数测试。如果其他人有建议,请提供。如果一组不起作用,我也对尝试不同 headers 的好的解决方案感兴趣。
req = urllib.request.Request(lnk['href'],
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
page = urllib.request.urlopen(req)
我试过你的代码,我得到了与预期相同的错误。
我也用我的 Chrome-Browser 提供的 User-Agent 试过了,这似乎有效
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
.. 还有 运行 没有通过显式 header 的测试也返回了 http 200(成功)。这将使用库提供的默认 header,例如
python-requests/2.10.0
希望对您有所帮助
HTTP 错误 406 不可接受
The HyperText Transfer Protocol (HTTP) 406 Not Acceptable client error response code indicates that the server cannot produce a response matching the list of acceptable values defined in the request's proactive content negotiation headers, and that the server is unwilling to supply a default representation.
所以我可以看出问题出在您的 User-Agent: Mozilla/5.0
键和值上。这是一堆正确的用户代理的链接,
所以将您的代码更改为以下内容,
headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'})
我知道答案为时已晚,但希望这对其他人有所帮助。