简体   繁体   English

如何使用mechanize cookies获取http页面?

[英]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: 有没有办法:

  1. Tell mechanize not to reset the page (perhaps, through UserAgentBase )? 告诉机械化不要重置页面(也许是通过UserAgentBase )?
  2. Make 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)不起作用
  3. Any other way to pass cookie to urllib ? 还有其他方法可以将cookie传递给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: 一些疯狂的想法:

  • Fetch the second page before filling in the form? 在填写表格之前获取第二页?
  • Or fetch the new page and then goBack()? 或者获取新页面然后goBack()? Although maybe that will reset the values. 虽然可能会重置这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM