2016-08-17 68 views
0

我已经通过了论坛搜索,试图找出为什么下面的代码不工作: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 

任何帮助,将不胜感激

+0

您是否考虑过使用'requests'而不是? –

+0

看起来像[urllib2.HTTPError:HTTP Error 403:Forbidden](https://stackoverflow.com/questions/13303449/urllib2-httperror-http-error-403-forbidden/46213623#46213623) – djinn

回答

3

此代码:

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() 

响应:

The Project Gutenberg EBook of Crime and Punishment, by Fyodor Dostoevsky

的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,这意味着你访问这个网址被禁止。

+0

看起来像OP正在使用Python 3.更新Python 3版本的urllib的答案,或者这可能会导致更多的混淆 –

+0

你说得对,我会编辑我的答案 – Reuven

+0

这确实有窍门!非常感谢你,这个网站很漂亮:) –