I am trying to make a script to login into my "check card balance" service for my university using python. Basically it's a web form where we fill-in our PIN and PASS and it shows us how much $$$ is left on our card (for food)...
This is the webpage: [url]http://www.wcu.edu/11407.asp[/url]
This is the form I am filling:
<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>
As you will see the fields i need to fill in are:
<input maxlength="12" size="12" type="password" name="id">
<input maxlength="20" type="password" name="PIN">
then I need to press the button:
<input value="Request Access" type="submit" name="submit">
When i do this in my browser, this takes me to another page where it shows my balance and id in simple html...
So I tried to write a python script that would give the the html of that page (so I could parse out the amount of money I have left and print it to the screen)
This is what I have so far:
import urllib
import urllib2
import sys
import cookielib
import hashlib
cookieJar = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))
opener.addheaders = [('User-agent', "Mozilla/5.0")]
username = "xxxxxxx"
password = "xxxxxxxx"
action = "https://itapp.wcu.edu/BanAuthRedirector/Default.aspx"
print hashlib.md5(hashlib.md5(password).hexdigest()).hexdigest()
#url = "http://www.wcu.edu/11407.asp"
url = "http://www.wcu.edu/11407.asp"
form = {"action" : action,
"id" : username,
"PIN" : password}
encodedForm = urllib.urlencode(form)
request = urllib2.Request(url, encodedForm)
page = opener.open(request)
contents = page.read()
f = open("mycatpage.txt", "w")
f.write(contents)
f.close()
Why does't this work???
Thanks in advance
EDIT I made a new version of the script, but it gives me an error:
Traceback (most recent call last):
File "checkbalance3.py", line 20, in <module>
open("mycatpage.html", 'w').write(opener.open(request))
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 438, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error
This is the code:
from urllib import urlopen, urlencode
import urllib2
myId = 'xxxxxxxx'
myPin = 'xxxxxxx'
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')]
url = 'https://itapp.wcu.edu/BanAuthRedirector/Default.aspx'
request = urllib2.Request(url, urlencode(data))
open("mycatpage.html", 'w').write(opener.open(request))
Any ideas?