geft geft - 1 year ago 68
HTTP Question

How do I prevent a python script from being redirected from a specific web page?

For example, I tried getting Python to read the following filtered page

http://www.hearthpwn.com/cards?filter-attack-val=1&filter-attack-op=1&display=1


but Python only gets the unfiltered page
http://www.hearthpwn.com/cards
instead.

Answer Source

The standard library urllib2 normally follows redirects. If retrieving this URL used to work without being redirected, then the site has changed.

Although you can prevent following the redirect within urllib2 (by providing an alternative HTTP handler), I recommend using requests, where you can do:

import requests
r = requests.get('http://www.hearthpwn.com/cards?filter-attack-val=1'
                 '&filter-attack-op=1&display=1', allow_redirects=False)
print(r)

giving you:

<Response [302]>