Description: Python's urllib2 fetch page (urlopen) is automatically redirected (301,302). However, sometimes we need to get status information for 302,301 pages. You must obtain debugging information prior to the turn.
The following code will be able to avoid 302 redirects to the new page
#!/usr/bin/python #-*-coding:utf-8-*-#encoding =utf-8 #Filename: states_code.py impor T Urllib2 class Redircthandler (urllib2. Httpredirecthandler): "" "DocString for Redircthandler" "Def http_error_301 (self, req, FP, code, MSG, headers): PA SS Def http_error_302 (self, req, FP, code, MSG, headers): Pass def getunredirecturl (url,timeout=10): req = Urlli B2. Request (URL) debug_handler = Urllib2. HttpHandler (DebugLevel = 1) opener = Urllib2.build_opener (Debug_handler, redircthandler) HTML = None response = No Ne try:response = opener.open (url,timeout=timeout) HTML = response.read () except URLLIB2. Urlerror as E:if hasattr (E, ' Code '): Error_info = E.code elif hasattr (E, ' reason '): Error_info = E.re Ason finally:if response:response.close () if Html:return html else:return error_info HTML = Getunredirecturl (' http://jb51.net ') print HTML