需求:因为urllib2自动处理了重定向,如果取到某一步重定向的Location字段?
重定义urllib2.HTTPRedirectHandler,把重定向过程中的Location都取出来,就OK,如下代码:
class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
RedURLs301 = []
RedURLs302 = []
def Getredurl301(self):
return SmartRedirectHandler.RedURLs301
def Getredurl302(self):
return SmartRedirectHandler.RedURLs302
def http_error_301(self, req, fp, code, msg, headers):
if headers.has_key("Location"):
SmartRedirectHandler.RedURLs301.append(headers["Location"])
result = urllib2.HTTPRedirectHandler.http_error_301(
self, req, fp, code, msg, headers)
return result
def http_error_302(self, req, fp, code, msg, headers):
if headers.has_key("Location"):
SmartRedirectHandler.RedURLs302.append(headers["Location"])
result = urllib2.HTTPRedirectHandler.http_error_302(
self, req, fp, code, msg, headers)
return result
opener = urllib2.build_opener(SmartRedirectHandler)