如何防止Python的urllib(2)跟随重定向

如何防止Python的urllib(2)跟随重定向,第1张

如何防止Python的urllib(2)跟随重定向

您可以做几件事:

  1. 构建自己的HTTPRedirectHandler来拦截每个重定向
  2. 创建HTTPcookieProcessor的实例并安装该打开程序,以便您可以访问cookiejar。

这是一件快速的小事,既显示了

import urllib2#redirect_handler = urllib2.HTTPRedirectHandler()class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):    def http_error_302(self, req, fp, pre, msg, headers):        print "cookie Manip Right Here"        return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, pre, 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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5668827.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存