【发布时间】:2020-02-19 14:44:34
【问题描述】:
我正在尝试使用 BeautifulSoup 和 urllib 获取 https://www.reddit.com/ 的 href 标签。这是我的代码;
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
searchurl = https://www.reddit.com/
html = urllib.request.urlopen(searchurl,context = ctx).read()
上面在某些情况下给了我 html,但在其他时候它会导致以下错误;
urllib.error.HTTPError: HTTP Error 401: Unauthorized
如何以一致的方式获取 html?
【问题讨论】:
-
这个错误告诉您您正在尝试访问您无权访问的页面。除了添加某种授权之外,您无能为力,
-
如果在访问 html 方面未经授权,则在某些情况下不会提供 html,而在其他情况下不会提供 html
标签: python beautifulsoup urllib