使用一個小腳本從網站獲取信息。我在使用HTTP錯誤時遇到問題。python urllib.request - 可能工作的標頭
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
周圍的Googling我發現,我應該修正標題(如我已經完成了上面)以及大量關於如何修復頭文件的教程。除了 - 沒有太多實際的工作。
是否有一些很好的標題可能不會導致大多數網站的問題?是否有一些其他人創建的Python模塊已經包含常用的頭文件?有沒有一種很好的方法可以多次重複使用不同的標題,直到獲得良好的響應?
這似乎是一個問題,每個人都在使用Python進行網頁抓取處理,而且我還沒有找到一個體面的解決方案。