我正在使用beautifulsoup4并使用python机械化库来自动化Web应用程序中的一些场景。基本上,我想执行以下步骤: 1)打开网站网址 2)输入用户名和密码,然后单击登录按钮 3)验证是否成功登录 4)注销。
下面是我执行上述步骤的代码:
import mechanize
import http.cookiejar as cookielib
from bs4 import BeautifulSoup
import html2text
LOGIN_URL = 'www.xyz.com/login.aspx'
UserName = 'Username'
Password = 'Password'
cj = cookielib.CookieJar()
br = mechanize.Browser()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Chrome')]
# The site we will navigate into, handling it's session
br.open(LOGIN_URL)
# Select the second (index one) form (the first form is a search query box)
br.select_form(nr=0)
# User credentials
br.form['txtUserId'] = UserName
br.form['txtPassword'] = Password
# Login
br.submit()
mainpage = br.response().read()
每一次,即使密码错误,我都会得到相同的响应,即状态码200。
任何帮助将不胜感激!