I am wondering how you can prevent urllib2 after requesting a redirect to my chosen URL. I found this piece of code while browsing, but it seems to work globally, and I just want it to disable redirection to a specific URL:
import urllib2 class RedirectHandler(urllib2.HTTPRedirectHandler): def http_error_302(self, req, fp, code, msg, headers): result = urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp) result.status = code return result http_error_301 = http_error_303 = http_error_307 = http_error_302 opener = urllib2.build_opener(RedirectHandler()) webpage = opener.open('http://www.website.com').geturl() print webpage
I should also mention that I am requesting a URL using urllib.urlopen ('site.com') and I want the first redirect to be allowed, for example, redirecting the site.com/redirect site, but then it tries redirect again from site.com/redirect to site.com/secondredirect. I would like the script to recognize "secondredirect" in the url and terminate this request. Hopefully I explained all this well and hopefully see some answers as I spent hours on hours trying to figure it out: headache:
redirect python urllib2
user2002290 Nov 12 '13 at 9:37 2013-11-12 09:37
source share