0

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???

Perun
  • 103
  • 1
  • 2
  • 7

2 Answers2

0

Why don't you just check where the error originates from by typing your code into a python shell? Or testing it with another site? There are a number of obvious possibilities to test against the reason of the error you face.

marsch
  • 33
  • 5
  • the error originates on the submit, that is exactly the problem, it works fine on other websites... That is why I posted this, maybe someone can find something I missed.... – Perun Apr 06 '12 at 18:52
0

Look into my problem here

Also an auto login for my university its page, with working code and html code as example.

Community
  • 1
  • 1
xrdty
  • 886
  • 2
  • 10
  • 22