[英]How to get a http page using mechanize cookies?
There is a Python mechanize object with a form with almost all values set, but not yet submitted. 有一个Python机械化对象,其表单几乎都设置了所有值,但尚未提交。 Now I want to fetch another page using cookies from mechanize instance, but without resetting the page, forms and so on, eg so that the values remain set (I just need to get body string of another page, nothing else).
现在我想使用来自mechanize实例的cookie获取另一个页面,但不重置页面,表单等,例如,以便值保持设置(我只需要获取另一个页面的正文字符串,没有别的)。 So is there a way to:
有没有办法:
UserAgentBase
)? UserAgentBase
)? urllib2
use mechanize's cookie jar? urllib2
使用mechanize的cookie罐? NB: urllib2.HTTPCookieProcessor(self.br._ua_handlers["_cookies"].cookiejar)
doesn't work urllib2.HTTPCookieProcessor(self.br._ua_handlers["_cookies"].cookiejar)
不起作用 urllib
? urllib
吗? And the correct answer: 而正确的答案是:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.br._ua_handlers["_cookies"].cookiejar))
opener.open(imgurl)
No idea whether this will work, but why don't you try deepcopying the mechanize instance, eg 不知道这是否有效,但为什么不尝试深入复制机械化实例,例如
from copy import deepcopy
br = Browser()
br.open("http://www.example.com/")
# Make a copy for doing other stuff with
br2 = deepcopy(br)
# Do stuff with br2
# Now do stuff with br
Some wild ideas: 一些疯狂的想法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.