Python urllib简单的登录脚本

时间:2012-04-06 00:58:48

标签: python login urllib2 urllib

我正在尝试使用python为我的大学登录我的“支票卡余额”服务。基本上它是一个网页表格,我们填写我们的密码和通行证,它向我们显示我们的卡上留下了多少$$$(用于食物)......

这是网页:[url] http://www.wcu.edu/11407.asp [/ url]

这是我填写的表格:

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

正如您将看到我需要填写的字段是:

<input maxlength="12" size="12" type="password" name="id">
<input maxlength="20" type="password" name="PIN">

然后我需要按下按钮:

<input value="Request Access" type="submit" name="submit">

当我在浏览器中执行此操作时,这会将我带到另一个页面,在那里它以简单的html显示我的余额和ID ...

所以我试着编写一个python脚本来提供该页面的html(所以我可以解析我剩下的钱并将其打印到屏幕上)

这是我到目前为止所做的:

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()

为什么这不起作用?

提前致谢

修改 我制作了一个新版本的脚本,但它给了我一个错误:

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

这是代码:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我试过这个(看起来很有效,至少没有例外):

from urllib import urlopen, urlencode
myId = '<your_id_here>'
myPin = '<your_pin_here>'
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())