我知道有很多人问过类似的问题,但在仔细研究了答案并遵循了这些技巧之后,我无法让这个脚本发挥作用。。。在
这是我的问题。。。我正在尝试编写一个python脚本,使用“mechanize”模块登录到我的大学“膳食平衡”页面,并获取显示我食物余额递减的页面的源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()
这会给我返回登录页面,而不是之后的页面。。。。为什么?在
看看我的问题here
同时也是我的大学自动登录它的网页,以工作代码和html代码为例。在
为什么不直接在pythonshell中输入代码来检查错误的来源呢?或者用另一个网站测试?有许多明显的可能性可以用来测试你所面临的错误的原因。在
相关问题 更多 >
编程相关推荐