【发布时间】:2015-08-09 22:16:35
【问题描述】:
所以我正在尝试在 python3 中获取页面的 URL...
如果我执行以下操作,
from urllib.request import urlopen
html = urlopen("http://google.com/")
html.read()
我根据需要得到了 html。 但是,如果我要选择不同的 url,如下所示,
from urllib.request import urlopen
html = urlopen("http://www.stackoverflow.com/")
html.read()
我在 second 行之后收到以下错误:
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
任何想法为什么会发生这种情况以及如何解决它?
【问题讨论】:
标签: python python-3.x urllib urlopen