3

我正在将 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)

补充:我也尝试过使用mechanize,但没有成功。这可能是一个新问题,但我会在这里提出,因为它是相同的最终目标:

这个使用 mechanize 的简单代码与 302 发射 url (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'

有任何想法吗?

4

4 回答 4

2

我最近遇到了完全相同的问题,但为了节省时间,我决定放弃它并决定使用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不能提供开箱即用的解决方案,或者至少没有很多调整,但如果你时间紧迫,只想让它工作,只使用机械化。

于 2011-04-04T21:05:20.140 回答
1

取决于重定向是如何完成的。如果它是通过 HTTP 刷新完成的,那么 mechanize 有一个可以使用的 HTTPRefreshProcessor。尝试像这样创建一个开瓶器:

cj = mechanize.CookieJar()
opener = mechanize.build_opener(
    mechanize.HTTPCookieProcessor(cj),
    mechanize.HTTPRefererProcessor,
    mechanize.HTTPEquivProcessor,
    mechanize.HTTPRefreshProcessor)
于 2011-06-12T12:31:03.533 回答
0

我刚刚得到了下面的变体,至少在尝试从http://www.fudzilla.com/home?format=feed&type=atom读取 Atom 时

我无法验证以下代码段是否会按原样运行,但可能会给您一个开始:

import cookielib
cookie_jar = cookielib.LWPCookieJar()
cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
handlers = [cookie_handler] #+others, we have proxy + progress handlers
opener = apply(urllib2.build_opener, tuple(handlers + [_FeedURLHandler()])) #see http://code.google.com/p/feedparser/source/browse/trunk/feedparser/feedparser.py#2848 for implementation of _FeedURLHandler
opener.addheaders = [] #may not be needed but see the comments around the link referred to below
try:
    return opener.open(request) #see http://code.google.com/p/feedparser/source/browse/trunk/feedparser/feedparser.py#2954 for implementation of request
finally:
    opener.close()
于 2011-08-11T01:56:09.853 回答
0

我也遇到了同样的问题,服务器将使用 302 和 Set-Cookie 标头中的会话令牌响应登录 POST 请求。使用 Wireshark 可以清楚地看到 urllib 遵循重定向但不包括 Cookie 中的会话令牌。

我实际上只是撕掉了 urllib 并直接用请求替换了它,它第一次完美地工作而无需更改任何东西。给那些家伙的大道具。

于 2016-02-03T22:27:27.757 回答