我正在使用带有CookieJar / HTTPCookieProcessor的liburl2来尝试模拟页面登录以自动上传。
我已经看到了一些问题和答案,但没有解决我的问题。当我模拟以302重定向结束的登录时,我正在丢失我的cookie。 302响应是服务器设置cookie的位置,但urllib2 HTTPCookieProcessor似乎在重定向期间不保存cookie。我尝试创建一个HTTPRedirectHandler类来忽略重定向,但这似乎没有办法。我尝试全局引用CookieJar来处理来自HTTPRedirectHandler的cookie,但是1.这不起作用(因为我正在处理重定向器的头,我正在使用的CookieJar函数,extract_cookies,需要一个完整的请求)和处理它是一种丑陋的方式。
我可能需要一些指导,因为我对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的简单代码,当与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'
有什么想法吗?
答案 0 :(得分: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
无法提供开箱即用的解决方案,或者至少没有经过多次调整,但如果你做的很短时间,只是希望它工作,只需使用机械化。
答案 1 :(得分:1)
取决于重定向的完成方式。如果它是通过HTTP刷新完成的,那么mechanize有一个你可以使用的HTTPRefreshProcessor。 尝试创建一个这样的开场白:
cj = mechanize.CookieJar()
opener = mechanize.build_opener(
mechanize.HTTPCookieProcessor(cj),
mechanize.HTTPRefererProcessor,
mechanize.HTTPEquivProcessor,
mechanize.HTTPRefreshProcessor)
答案 2 :(得分: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()
答案 3 :(得分:0)
我也遇到了同样的问题,服务器将使用302和Set-Cookie标头中的会话令牌响应登录POST请求。使用Wireshark可以清楚地看到urllib遵循重定向但不包括Cookie中的会话令牌。
我真的只是撕掉了urllib并直接替换了requests并且它第一次完美地工作而不必改变一件事。那些人的大道具。