![](/img/trans.png)
[英]Web scraping using python: urlopen returns HTTP Error 403: Forbidden
[英]Forbidden (403) error in Request,urlopen in Python 3.5
我正在嘗試使用以下代碼在Python 3.5中訪問URL http://learncodethehardway.org/words.txt ,但看到403 HTTP錯誤。 我應該如何配置headers
以避免這種情況?
import urllib.request
url = "http://learncodethehardway.org/words.txt"
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
req = urllib.request.Request(url, None, headers=hdr)
html = urllib.request.urlopen(req)
print (html.read())
這對我有用:
import urllib.request
url = 'http://learncodethehardway.org/words.txt'
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
html = urllib.request.urlopen(req).read()
print(html.decode())
有關更多信息,請參見類似的問題: Python 3 Web Scraping中的HTTP錯誤403
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.