编程语言
首页 > 编程语言> > python-302s和urllib2丢失cookie

python-302s和urllib2丢失cookie

作者:互联网

我正在将liburl2与CookieJar / HTTPCookieProcessor结合使用,以尝试模拟登录页面以自动上传.

我已经看到了一些问题和答案,但是没有什么能解决我的问题.当我模拟登录最终以302重定向结束时,我丢失了cookie. 302响应是服务器设置cookie的位置,但是urllib2 HTTPCookieProcessor似乎在重定向期间未保存cookie.我尝试创建一个HTTPRedirectHandler类来忽略重定向,但这似乎并没有解决问题.我尝试全局引用CookieJar来处理来自HTTPRedirectHandler的cookie,但是1.这不起作用(因为我正在处理重定向器的标头,而我正在使用的CookieJar函数extract_cookies需要完整的请求),并且2.这是处理它的丑陋方法.

我可能需要一些指导,因为我对Python相当了解.我想我主要是在这里树正确的树,但也许专注于错误的分支.

cj = cookielib.CookieJar()
cookieprocessor = urllib2.HTTPCookieProcessor(cj)


class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
  def http_error_302(self, req, fp, code, msg, headers):
    global cj
    cookie = headers.get("set-cookie")
    if cookie:
      # Doesn't work, but you get the idea
      cj.extract_cookies(headers, req)

    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(cj)

# Oh yeah.  I'm using a proxy too, to follow traffic.
proxy = urllib2.ProxyHandler({'http': '127.0.0.1:8888'})
opener = urllib2.build_opener(MyHTTPRedirectHandler, cookieprocessor, proxy)

另外:我也尝试过使用机械化,但没有成功.这可能是一个新问题,但由于它是相同的最终目标,因此在此提出:

当与302发射URL(http://fxfeeds.mozilla.com/firefox/headlines.xml)一起使用时,这种使用机械化的简单代码(http://fxfeeds.mozilla.com/firefox/headlines.xml)-请注意,当不使用set_handle_robots(False)时也会发生相同的行为.我只是想确保不是:

import urllib2, mechanize

browser = mechanize.Browser()
browser.set_handle_robots(False)
opener = mechanize.build_opener(*(browser.handlers))
r = opener.open("http://fxfeeds.mozilla.com/firefox/headlines.xml")

输出:

Traceback (most recent call last):
  File "redirecttester.py", line 6, in <module>
    r = opener.open("http://fxfeeds.mozilla.com/firefox/headlines.xml")
  File "build/bdist.macosx-10.6-universal/egg/mechanize/_opener.py", line 204, in open
  File "build/bdist.macosx-10.6-universal/egg/mechanize/_urllib2_fork.py", line 457, in http_response
  File "build/bdist.macosx-10.6-universal/egg/mechanize/_opener.py", line 221, in error
  File "build/bdist.macosx-10.6-universal/egg/mechanize/_urllib2_fork.py", line 332, in _call_chain
  File "build/bdist.macosx-10.6-universal/egg/mechanize/_urllib2_fork.py", line 571, in http_error_302
  File "build/bdist.macosx-10.6-universal/egg/mechanize/_opener.py", line 188, in open
  File "build/bdist.macosx-10.6-universal/egg/mechanize/_mechanize.py", line 71, in http_request
AttributeError: OpenerDirector instance has no attribute '_add_referer_header'

有任何想法吗?

解决方法:

我最近遇到了完全相同的问题,但是为了节省时间,决定使用mechanize.它可以完全替代urllib2,其行为与您期望浏览器针对Referer标头的行为完全相同,重定向和Cookie.

import mechanize
cj = mechanize.CookieJar()
browser = mechanize.Browser()
browser.set_cookiejar(cj)
browser.set_proxies({'http': '127.0.0.1:8888'})

# Use browser's handlers to create a new opener
opener = mechanize.build_opener(*browser.handlers)

Browser对象本身可用作打开器(使用.open()方法).它在内部维护状态,但在每次调用时都返回一个响应对象.这样您将获得很大的灵活性.

另外,如果您不需要手动检查cookiejar或将其传递给其他对象,则也可以省略该对象的显式创建和分配.

我完全知道这不能解决真正的问题,为什么urllib2不能开箱即用或至少没有大量调整就不能提供此解决方案,但是如果您时间紧迫,只是想让它工作,只需使用机械化即可.

标签:urllib2,mechanize,cookiejar,python
来源: https://codeday.me/bug/20191208/2092349.html