Urllib HTTP Error 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])

但是到目前为止都没能解决问题。以下是我尝试过的一些类似的解决方案,但都无济于事:论坛1,论坛2

我得到的错误是:
 File "C:Python33liburllibrequest.py", line 163, in urlopen
return opener.open(url, data, timeout)
 File "C:Python33liburllibrequest.py", line 472, in open
response = meth(req, response)
 File "C:Python33liburllibrequest.py", line 582, in http_response
'http', request, response, code, msg, hdrs)
 File "C:Python33liburllibrequest.py", line 510, in error
return self._call_chain(*args)
 File "C:Python33liburllibrequest.py", line 444, in _call_chain
result = func(*args)
 File "C:Python33liburllibrequest.py", line 590, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

如有任何帮助,不胜感激

下面的代码可以运行:

Python 2

from  urllib import urlopen
url = "http://www.gutenberg.org/files/2554/2554.txt"
response = urlopen(url)
if response.code == 200:
    raw = response.read().decode('utf-8')
    print raw[:75]
else:
    print 'Error', response.code
response.close()

反应:

古腾堡计划罪与罚电子书,费奥多尔·陀思妥耶夫斯基著

Python 3

from  urllib import request
url = "http://www.gutenberg.org/files/2554/2554.txt"
try:
    response = request.urlopen(url)
    raw = response.read().decode('utf-8')
    print(raw[:75])
except Exception as ex:
    print('Error:', ex)

如果你得到HTTP代码403,这意味着你禁止访问这个url

相关内容

  • 没有找到相关文章

最新更新