禁止(403)请求错误,Python3.5中的urlopen

2024-09-27 00:20:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试下面的代码来访问python3.5中的URL http://learncodethehardway.org/words.txt,但是我看到了一个403http错误。我应该如何配置我的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())

Tags: orgtxthttpurlhdrapplicationrequesthtml
1条回答
网友
1楼 · 发布于 2024-09-27 00:20:33

这对我有用:

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())

有关更多信息,请参阅类似问题: HTTP error 403 in Python 3 Web Scraping

相关问题 更多 >

    热门问题