遵循了urlib.request.urlopen文档,但仍然不起作用



我遵循了文档,但仍然遇到了无法解决的错误。我使用的是Python 3。

这是我的代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen('http://pythonscraping.com/pages/page1.html')
bs = BeautifulSoup(html.read(), "html.parser")
print(bs.h1)

带有代码和错误的代码编辑器

你做得很好。

您提供的URL使用HTTPS,您收到的错误与网站上的证书问题有关。

如果你想学习新东西,只需将URL更改为其他示例网站即可。

如果您想从一个特定的URL获得结果,而不管代价如何,请将关键字参数context添加到您的urlopen调用中,并为其提供正确的SSL上下文:

from ssl import create_default_context, CERT_NONE
from urllib.request import urlopen
from bs4 import BeautifulSoup
context = create_default_context()
context.verify_mode = ssl.CERT_NONE
html = urlopen('http://pythonscraping.com/pages/page1.html', context=context)
bs = BeautifulSoup(html.read(), "html.parser")
print(bs.h1)

最新更新