我已經通過了論壇搜索,試圖找出爲什麼下面的代碼不工作:urllib的HTTP錯誤403
import nltk, re, pprint
from urllib import request
url = "http://www.gutenberg.org/files/2554/2554.txt"
response = request.urlopen(url)
raw = response.read().decode('utf8')
print(raw[:75])
但迄今爲止,已經解決的事情不成功。這裏有一些類似的解決方案,我試圖執行無濟於事: Forum 1, Forum 2
我得到的錯誤是:
File "C:\Python33\lib\urllib\request.py", line 163, in urlopen
return opener.open(url, data, timeout)
File "C:\Python33\lib\urllib\request.py", line 472, in open
response = meth(req, response)
File "C:\Python33\lib\urllib\request.py", line 582, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python33\lib\urllib\request.py", line 510, in error
return self._call_chain(*args)
File "C:\Python33\lib\urllib\request.py", line 444, in _call_chain
result = func(*args)
File "C:\Python33\lib\urllib\request.py", line 590, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
任何幫助,將不勝感激
您是否考慮過使用'requests'而不是? –
看起來像[urllib2.HTTPError:HTTP Error 403:Forbidden](https://stackoverflow.com/questions/13303449/urllib2-httperror-http-error-403-forbidden/46213623#46213623) – djinn