Pythonを使用して、大学の「チェックカード残高」サービスにログインするためのスクリプトを作成しようとしています。基本的には、PINとPASSを入力するWebフォームであり、カードに残っている$$$(食品用)の量が表示されます...
これはウェブページです:[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">
ブラウザでこれを行うと、別のページに移動し、バランスとIDが単純なHTMLで表示されます...
そこで、そのページのhtmlを表示するPythonスクリプトを作成しようとしました(残りの金額を解析して画面に出力できるようにしました)
これは私がこれまでに持っているものです:
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))
何か案は?