好問題!如果您使用的是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