【问题标题】:Need to create a persistence session in python using mechanize browser instance需要使用机械化浏览器实例在 python 中创建持久性会话
【发布时间】:2013-07-09 07:39:28
【问题描述】:
import mechanize
import urllib2
import time
import cookielib
import requests

username = 'user@gmail.com'   # your username/email
password = 'pwd'   # your password

br = mechanize.Browser()

# browser settings (used to emulate a browser)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_debug_http(False)
br.set_debug_responses(False)
br.set_debug_redirects(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time = 1)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

br.open('https://sso.openx.com/login/login') # open twitter

br.select_form(nr=0) # select the form

br['email'] = username
br['password'] = password
br.submit() # submit the login data
    # set cookies
cookies = cookielib.LWPCookieJar()
br.set_cookiejar(cookies)
print cookies
temp_jar=br.set_cookiejar(cookies)

print(br.response().read()) # print the response

br1=mechanize.Browser()
br1.set_cookiejar(cookies)
br1.set_handle_robots(False)
r=br1.open('http://ox-ui.admin.one97adworks.com/ox/3.0/a/report/run?&report_format=json&end_date=2013-06-01&report=order_perf&do_break=&rollup=hourly&start_date=2013-06-01')
print r.text

基本上我有 2 个 URL,我必须先登录(“https://sso.openx.com/login/login”) 使用正确的凭据登录后,我将可以访问另一个网址(“http://ox-ui.admin.one97adworks.com/ox/3.0/a/report/run?&report_format=json&end_date=2013-06-01&report=order_perf&do_break=&rollup=hourly&start_date=2013-06-01”) 我必须获取 URL 返回的 json

所以基本上我想要一个持久会话,我可以使用它登录然后访问我能够成功登录但无法使用相同会话或 cookie 或任何其他持久性方法调用其他 url两个电话

【问题讨论】:

  • 是否需要创建另一个机械化浏览器实例?
  • 不,这不是必需的,但它也不能在同一个实例中工作

标签: python session-cookies mechanize


【解决方案1】:

尝试在打开网页之前定义cookiejar。并且只使用一个 mechanize 实例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 2023-03-26
    • 2012-11-22
    相关资源
    最近更新 更多