dangus poochie
dangus poochie

Reputation: 103

Urllib HTTP Error 403

I've searched through the forums to try and figure out why the following code was not working:

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

But have thus far been unsuccessful in resolving things. Here are some similar solutions I tried to implement to no avail: Forum 1, Forum 2

The error I get is:

 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

Any help would be greatly appreciated

Upvotes: 0

Views: 1738

Answers (1)

Reuven
Reuven

Reputation: 43

This code works:

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

Response:

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)

If you get HTTP code 403, it mean that you forbidden from access this url.

Upvotes: 3

Related Questions