1

我正在尝试使用 python 为我的大学制作一个脚本来登录我的“检查卡余额”服务。基本上它是一个网络表格,我们在其中填写我们的 PIN 和 PASS,它显示我们的卡上还剩下多少 $$$(用于食物)......

这是网页:[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))

有任何想法吗?

4

1 回答 1

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())
于 2012-04-06T01:27:48.783 回答