好问题!如果您使用的是urllib2
,那么您需要查看this answer了解您自己的重定向处理程序的构建。
在短(读:从以前的答案公然被盗):
import urllib2
#redirect_handler = urllib2.HTTPRedirectHandler()
class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
print "Cookie Manip Right Here"
return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
http_error_301 = http_error_303 = http_error_307 = http_error_302
cookieprocessor = urllib2.HTTPCookieProcessor()
opener = urllib2.build_opener(MyHTTPRedirectHandler, cookieprocessor)
urllib2.install_opener(opener)
response =urllib2.urlopen("WHEREEVER")
print response.read()
print cookieprocessor.cookiejar
而且,在勘误表中提到,您可以使用Python Requests。
[要求](http://docs.python-requests.org/en/latest/index.html)至少声称这样做的正确的方式(至少,它将重定向行为记录为幂等方法,并在文档中专门调用HEAD)。 – 2012-04-01 19:41:25
类似的解决方案:http://stackoverflow.com/questions/9890815/python-get-headers-only-using-urllib2/9892207#9892207 – newtover 2012-04-01 21:00:21