Python urllib简单登录脚本 [英] Python urllib simple login script

查看:119
本文介绍了Python urllib简单登录脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写脚本以使用python登录到我的大学的支票余额"服务.基本上,这是一个网络表单,我们在其中填写PIN码和通行证,并向我们显示卡上剩下的$$$$$(用于食品)...

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

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

This is the webpage: [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>

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

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

然后我需要按下按钮:

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

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

When i do this in my browser, this takes me to another page where it shows my balance and id in simple html...

所以我尝试编写一个Python脚本,该脚本将提供该页面的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()

为什么这行不通?

预先感谢

编辑 我制作了该脚本的新版本,但它给了我一个错误:

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

这是代码:

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

有什么想法吗?

推荐答案

我尝试了此操作(似乎正常,至少没有例外):

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

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

这篇关于Python urllib简单登录脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆