2016-08-17 86 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

這確實有竅門!非常感謝你,這個網站很漂亮:) –