Peilin Yang
Peilin Yang

Reputation: 75

How to handle with Httperror 303 in Python 2.7?

When I run the following code:

url = 'http://www.nytimes.com/2011/11/15/arts/music/new-music-from-caveman-los-campesinos-and-the-fall.html?_r=0'
try:
    handle = urllib2.urlopen(url).info()    
except urllib2.HTTPError, e:
    print(e.code)

There is an error and print e.code prints 303. If I request this URL using Chrome or Firefox, it works fine.

Anyone can help? Thanks

Upvotes: 3

Views: 2106

Answers (2)

Thomas Orozco
Thomas Orozco

Reputation: 55283

You will need to handle the redirection because HTTP 303 is a "See Other" response. The location where the content is at will be given to you in the Location header:

>>> e.headers['Location']
'http://www-nc.nytimes.com/2011/11/15/arts/music/new-music-from-caveman-los-campesinos-and-the-fall.html?=_r=6&'

Now, using urllib / urllib2 today is really asking for pain, what you should actually do is use the excellent requests library, which will handle everything for you.

I think we can say that using requests is the correct way of doing HTTP in Python:

>>> res = requests.get(url)
<Response [200]>
>>> print res.text
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" # And so on

Upvotes: 4

Sheena
Sheena

Reputation: 16242

303 is a redirect. Your browser handles it automatically, urllib2 needs some coaxing.

Check this out for a very good explanation:

http://www.diveintopython.net/http_web_services/redirects.html

Upvotes: 0

Related Questions