我知道很多人问过类似的问题,但是在查看了答案并遵循了这些提示之后,我无法让这个脚本工作......
这是我的问题......我正在尝试使用“mechanize”模块编写一个python脚本来登录我的大学“膳食平衡”页面并获取显示我的食物余额下降页面的源html,然后我会解析 html 源代码并获取数字...
问题是访问上述网页并登录...
这是登录网站:http: //www.wcu.edu/11407.asp 最后你会看到我需要填写的表格...
这是我尝试使用的代码,用于登录并获取余额下降的页面:
import mechanize, cookielib
from time import sleep
url = 'http://www.wcu.edu/11407.asp'
myId = 'xxxxxxxx'
myPin = 'xxxxxxxx'
# Browser
#br = mechanize.Browser()
#br = mechanize.Browser(factory=mechanize.DefaultFactory(i_want_broken_xhtml_support=True))
br = mechanize.Browser(factory=mechanize.RobustFactory()) # Use this because of bad html
# Cookie Jar
cj = cookielib.LWPCookieJar()
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)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# User-Agent (fake agent to google-chrome linux x86_64)
br.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'),
('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
('Accept-Encoding', 'gzip,deflate,sdch'),
('Accept-Language', 'en-US,en;q=0.8'),
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
# The site we will navigate into, handling it's session
br.open(url)
for f in br.forms():
print f
# Select the third (index two) form
br.select_form(nr=2)
# User credentials
br.form['id'] = myId
br.form['PIN'] = myPin
br.form.action = 'https://itapp.wcu.edu/BanAuthRedirector/Default.aspx
# Login
res = br.submit().read()
sleep(10)
f = file('mycatpage.html', 'w')
f.write(res)
f.close()
这给了我登录页面,而不是之后的页面......为什么?