user1452494
user1452494

Reputation: 1185

urlopen(url) 403 Forbidden error

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

Forbidden

\n

You don\'t have permission to access /files/2554/2554.txt\non this server.

\n
\nApache Server at www.gutenberg.org Port 80\n\n'

What is this?

Thank you

Upvotes: 1

Views: 1164

Answers (1)

rwolst
rwolst

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

Related Questions