python urllib2 解决重定向问题

来源:互联网 发布:linux文件分别打包命令 编辑:程序博客网 时间:2024/06/01 13:23

原文地址:https://stackoverflow.com/questions/554446/how-do-i-prevent-pythons-urllib2-from-following-a-redirect

由于urllib2处理重定向不会自动带上cookie,这个是比较麻烦的,找了几篇文章,还是这个靠谱,加上去以备万一。

1 以下代码是使得urllib2拥有重定向带上cookie

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_302cookieprocessor = urllib2.HTTPCookieProcessor()opener = urllib2.build_opener(MyHTTPRedirectHandler, cookieprocessor)urllib2.install_opener(opener)response =urllib2.urlopen("WHEREEVER")print response.read()print cookieprocessor.cookiejar

2 以下代码是阻止重定向,并且获得响应页面(非重定向之后的页面)。

class MyHTTPErrorProcessor(urllib2.HTTPErrorProcessor):    def http_response(self, request, response):        code, msg, hdrs = response.code, response.msg, response.info()        # only add this line to stop 302 redirection.        if code == 302: return response        if not (200 <= code < 300):            response = self.parent.error(                'http', request, response, code, msg, hdrs)        return response    https_response = http_responsecj = cookielib.CookieJar()opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), MyHTTPErrorProcessor)

class NoRedirection(urllib2.HTTPErrorProcessor):    def http_response(self, request, response):        return response    https_response = http_response

cj = cookielib.CookieJar()opener = urllib2.build_opener(NoRedirection, urllib2.HTTPCookieProcessor(cj))data = {}response = opener.open('http://www.example.com', urllib.urlencode(data))if response.code == 302:    redirection_target = response.headers['Location']
这里响应头部的Location字段就是重定向的url。

接下来的文章会利用这两个测试学校图书馆用户登录的问题