403 status code while trying to access webpage from Python

87 Views Asked by At

I've already tried using JSON but can't really read this page.

This is my python code. I've tried it on other websites and it works, but on this website it returns a 403.

import urllib2

req = urllib2.Request('http://www.taringa.net/envivo/ajax.php')
response = urllib2.urlopen(req)
the_page = response.read()

print the_page
2

There are 2 best solutions below

1
On BEST ANSWER

You have to add the 'User-Agent' header in order to make this work.

Urllib code:

req = urllib2.Request('http://www.taringa.net/envivo/ajax.php')
req.add_header('User-Agent', 'Mozilla')
resp = urllib2.urlopen(req)
print resp.code  # Gives 200.
print resp.read()  # Gives the HTML of the page.

I would recommend that you use requests mainly because it makes this kind of stuff very easy.

Requests code:

h = {'User-Agent':'Mozilla'}
requests.get('http://www.taringa.net/envivo/ajax.php', headers=h)
4
On

Better use requests. I tried your script and got the status of 403. This means that access to it is closed, for whatever reason, I do not know.