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或任何其他持久性方法之间调用其他网址这两个电话
答案 0 :(得分:0)
尝试在之前定义cookiejar 以打开网页。并且只使用一个机械化实例