各位程序员好!
我正在尝试编写一个脚本来使用 python 和机械化模块登录我的大学“食物平衡”页面......
这是我要登录的页面:http ://www.wcu.edu/11407.asp 该网站有以下登录表格:
<FORM method=post action=https://itapp.wcu.edu/BanAuthRedirector/Default.aspx><INPUT value=https://cf.wcu.edu/busafrs/catcard/idsearch.cfm type=hidden name=wcuirs_uri>
<P><B>WCU ID Number<BR></B><INPUT maxLength=12 size=12 type=password name=id> </P>
<P><B>PIN<BR></B><INPUT maxLength=20 type=password name=PIN> </P>
<P></P>
<P><INPUT value="Request Access" type=submit name=submit> </P></FORM>
由此我们知道我需要填写以下字段: 1. name=id 2. name=PIN
随着行动:action=https://itapp.wcu.edu/BanAuthRedirector/Default.aspx
这是我到目前为止写的脚本:
#!/usr/bin/python2 -W ignore
import mechanize, cookielib
from time import sleep
url = 'http://www.wcu.edu/11407.asp'
myId = '11111111111'
myPin = '22222222222'
# 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 tags in the 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
br.open(url)
# Go though all the forms (for debugging only)
for f in br.forms():
print f
# Select the first (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
br.submit()
# Wait 10 seconds
sleep(10)
# Save to a file
f = file('mycatpage.html', 'w')
f.write(br.response().read())
f.close()
现在问题...
出于某种奇怪的原因,我返回的页面(在 mycatpage.html 中)是登录页面,而不是显示我的“猫现金余额”和“剩余餐食数量”的预期页面......
有谁知道为什么?请记住,头文件的所有内容都是正确的,虽然 id 和 pass 不是真正的 111111111 和 222222222,但正确的值确实适用于网站(使用浏览器......)
提前致谢
编辑
我试过的另一个脚本:
from urllib import urlopen, urlencode
import urllib2
import httplib
url = 'https://itapp.wcu.edu/BanAuthRedirector/Default.aspx'
myId = 'xxxxxxxx'
myPin = 'xxxxxxxx'
data = {
'id':myId,
'PIN':myPin,
'submit':'Request Access',
'wcuirs_uri':'https://cf.wcu.edu/busafrs/catcard/idsearch.cfm'
}
opener = urllib2.build_opener()
opener.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')]
request = urllib2.Request(url, urlencode(data))
open("mycatpage.html", 'w').write(opener.open(request))
这具有相同的行为...