Python實現CET查分的方法_python

來源:互聯網
上載者:User

本文執行個體講述了Python實現CET查分的方法。分享給大家供大家參考。具體實現方法如下:

複製代碼 代碼如下:
#!/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..."
    #構造HTTP頭
    header = {'Referer':'http://cet.99sushe.com/'}
    #第二個參數出現則使用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 = ("聽力", "閱讀", "綜合", "寫作", "總分", "學校", "姓名")
    print "n***** CET %s 成績清單 *****" % (band)
    print "-准考證號: %s" % (exam_id)
    for i in range(7):
        print "-%s: %s" % (score_tu, res_tu)
    print "**************************n"
    print "准考證號前一位同學: %sn後兩位同學分別是: %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: 程式參數錯誤,考試類型(4、6),准考證號長度(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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.