I know quite a few people asked a similar question like this, but after looking though the answers and following this tips I can't get this script to work...
Here is my problem... I am trying to write a python script using the "mechanize" module to login into my university "meal balance" page and get the source html of the page that displays my declining balance for food, then I will parse the html source and get the numbers...
The problem is accessing the said webpage and logging in...
This is the login website: http://www.wcu.edu/11407.asp Towards the end you will see the FORM I need to fill...
Here is the code I am trying to use in order to login and get the page with my declining balance:
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()
This gives me the login page back and not the page after.... Why???