我正在登录一个网站并尝试在保持登录状态的同时更改其他功能中的URL。当我更改页面时,它会恢复到登录页面(好像从未登录过)。我不确定我做错了什么。我使用直接URL重定向,因此我无需导航表单来更改页面。
from bs4 import BeautifulSoup
import cookielib
from mechanize import Browser
import urllib2
#login information
username = 'user'
password = 'password'
#creates browser
def browser():
br = Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_robots(False)
return br
#connect and login to site
class connSite:
def login(self):
url = "https://www.website.com"
br = browser()
br.open(url)
#find login form
br.form = list(br.forms())[0]
control = br.form.find_control("login")
control.readonly = False
#pass login
br['username'] = username
#submit login
response = br.submit()
#find password form
br.form = list(br.forms())[0]
control = br.form.find_control("password")
control.readonly = False
#pass password
br['password'] = password
#submit login
response = br.submit()
return response
#gets first logged in page
def loginPage():
conn = connSite()
conn.login()
bs = BeautifulSoup(conn.login())
return bs
#get balance
def balance():
page = loginPage()
# find balance
elements = page.find(class_="col notFirst")
balance = elements.find("h1")
for bal in balance:
balance = bal
balance.replace("<h1>", "")
balance.replace("</h1>", "")
return balance
#pay
def payPage():
url = "https://www.website.com/servicing/Payment.action"
br = browser()
loginPage()
br.open(url)
form = list(br.forms())[0]
for f in br.forms():
print f.name
payPage()