Reputation: 1185
I'm using python to open an URL with the following code and sometimes I get this error:
from urllib import urlopen url = "http://www.gutenberg.org/files/2554/2554.txt" raw = urlopen(url).read()
error:'\n\n403 Forbidden\n\n
You don\'t have permission to access /files/2554/2554.txt\non this server.
\nWhat is this?
Thank you
Upvotes: 1
Views: 1164
Reputation: 13682
This is the web page blocking Python access as it is making requests with the header 'User-Agent'.
To get around this, download the 'urllib2' module and use this code:
req = urllib2.Request(url, headers ={'User-Agent':'Chrome'})
raw = urllib2.urlopen(req).read()
You are know accessing the site with the header 'Chrome' and should no longer be forbidden (I tried it myself and it worked).
Hope this helps.
Upvotes: 2