pythonurllib登录是什么_Python urllib简单登录脚本

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:

WCU ID Number

PIN

As you will see the fields i need to fill in are:

then I need to press the button:

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

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?

解决方案

I tried this (seems working, at least no exception):

from urllib import urlopen, urlencode

myId = ''

myPin = ''

data = {

'id':myId,

'PIN':myPin,

'submit':'Request Access',

'wcuirs_uri':'https://cf.wcu.edu/busafrs/catcard/idsearch.cfm'

}

url = 'https://itapp.wcu.edu/BanAuthRedirector/Default.aspx'

response = urlopen(url, urlencode(data))

open("mycatpage.txt",'w').write(response.read())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值