【发布时间】:2017-08-09 08:22:12
【问题描述】:
您好,不是每次,但有时在尝试访问 LSE 代码时,我会遇到每个烦人的 HTTP 错误 403:禁止消息。
任何人都知道我如何仅使用标准 python 模块来解决这个问题(遗憾的是没有漂亮的汤)。
import urllib.request
url = "http://www.londonstockexchange.com/exchange/prices-and-markets/stocks/indices/ftse-indices.html"
infile = urllib.request.urlopen(url) # Open the URL
data = infile.read().decode('ISO-8859-1') # Read the content as string decoded with ISO-8859-1
print(data) # Print the data to the screen
然而,这就是我经常看到的错误:
Traceback (most recent call last):
File "/home/ubuntu/workspace/programming_practice/Assessment/Summative/removingThe403Error.py", line 5, in <module>
webpage = urlopen(req).read().decode('ISO-8859-1')
File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.4/urllib/request.py", line 469, in open
response = meth(req, response)
File "/usr/lib/python3.4/urllib/request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.4/urllib/request.py", line 507, in error
return self._call_chain(*args)
File "/usr/lib/python3.4/urllib/request.py", line 441, in _call_chain
result = func(*args)
File "/usr/lib/python3.4/urllib/request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
Process exited with code: 1
链接到所有可用模块的列表:https://docs.python.org/3.4/py-modindex.html
非常感谢。
【问题讨论】:
-
只是想知道,您找到解决方案了吗?
标签: python python-3.x urllib http-status-code-403 urllib3