代碼如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, urllib2
def CetQuery(band, exam_id):
"""CETQuery version 0.2 2009.2.28
An Exercise Program by PT, GZ University
Author Blog: http://apt-blog.co.cc , Welcome to Drop by.
"""
#查詢連接
cet = "http://cet.99sushe.com/cetscore_99sushe0902.html?t=" + band + "&id=" + exam_id
print "Connecting..."
#構(gòu)造HTTP頭
header = {'Referer':'http://cet.99sushe.com/'}
#第二個參數(shù)出現(xiàn)則使用post方式提交
req = urllib2.Request(cet, '', header)
try:
data = urllib2.urlopen(req).read()
except BaseException, e:
print "Error retrieving data:", e
return -1
if not len(result):
print "Error Occured. Maybe record not existed."
return -1
#解碼字符串
result = data.decode("gb2312").encode("utf8")
res_tu = tuple(result.split(','))
score_tu = ("聽力", "閱讀", "綜合", "寫作", "總分", "學(xué)校", "姓名")
print "n***** CET %s 成績清單 *****" % (band)
print "-準(zhǔn)考證號: %s" % (exam_id)
for i in range(7):
print "-%s: %s" % (score_tu, res_tu)
print "**************************n"
print "準(zhǔn)考證號前一位同學(xué): %sn后兩位同學(xué)分別是: %s、%s" % (res_tu[-3], res_tu[-2], res_tu[-1])
return 0
if __name__ == "__main__":
if (len(sys.argv) != 3) or
(sys.argv[1] != '4' and sys.argv[1] != '6') or
(len(sys.argv[2]) != 15):
print "Error: 程序參數(shù)錯誤,考試類型(4、6),準(zhǔn)考證號長度(15位)"
print "nExample:nnCETQuery.py 4 123456789012345nn"
print CetQuery.__doc__
sys.exit(1)
statue = CetQuery(sys.argv[1], sys.argv[2])
sys.exit(statue)
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com