urlopen仅适用于Python3中的某些url

2024-03-28 16:19:31 发布

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

所以我试着在python3中获取一个页面的URL。。。在

如果我做了以下事情

from urllib.request import urlopen
html = urlopen("http://google.com/")
html.read()

我得到了想要的html。 但是,如果我选择一个不同的url,如下面所示

^{pr2}$

第二行之后,我得到以下错误:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 153, in urlopen return opener.open(url, data, timeout) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 461, in open response = meth(req, response) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 574, in http_response 'http', request, response, code, msg, hdrs) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 499, in error return self._call_chain(*args) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 433, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 582, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbidden

你知道为什么会发生这种情况,以及如何解决它吗?在


Tags: inpyhttpresponserequestlibhtmlline
1条回答
网友
1楼 · 发布于 2024-03-28 16:19:31

如果仔细查看错误消息,您会发现它是一个HTTP错误,而且是一个特殊错误:

HTTP Error 403: Forbidden

所以你和服务器谈过了,得到了回复,但你不知道为什么被拒绝了。在

您可以使用服务器返回的HTML格式获取更详细的消息,如下所示:

^{pr2}$

对我来说它说:

<h2 data-translate="what_happened">What happened?</h2>
<p>The owner of this website (www.stackoverflow.com) has banned your access based on your browser's signature (213702c58d2116a6-ua48).</p>

您可以将HTTPError视为文件对象(https://docs.python.org/3/library/urllib.error.html#urllib.error.HTTPError):

Though being an exception (a subclass of URLError), an HTTPError can also function as a non-exceptional file-like return value (the same thing that urlopen() returns). This is useful when handling exotic HTTP errors, such as requests for authentication.

相关问题 更多 >