search
HomeBackend DevelopmentPython TutorialPython实现CET查分的方法

本文实例讲述了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程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you slice a Python array?How do you slice a Python array?May 01, 2025 am 12:18 AM

The basic syntax for Python list slicing is list[start:stop:step]. 1.start is the first element index included, 2.stop is the first element index excluded, and 3.step determines the step size between elements. Slices are not only used to extract data, but also to modify and invert lists.

Under what circumstances might lists perform better than arrays?Under what circumstances might lists perform better than arrays?May 01, 2025 am 12:06 AM

Listsoutperformarraysin:1)dynamicsizingandfrequentinsertions/deletions,2)storingheterogeneousdata,and3)memoryefficiencyforsparsedata,butmayhaveslightperformancecostsincertainoperations.

How can you convert a Python array to a Python list?How can you convert a Python array to a Python list?May 01, 2025 am 12:05 AM

ToconvertaPythonarraytoalist,usethelist()constructororageneratorexpression.1)Importthearraymoduleandcreateanarray.2)Uselist(arr)or[xforxinarr]toconvertittoalist,consideringperformanceandmemoryefficiencyforlargedatasets.

What is the purpose of using arrays when lists exist in Python?What is the purpose of using arrays when lists exist in Python?May 01, 2025 am 12:04 AM

ChoosearraysoverlistsinPythonforbetterperformanceandmemoryefficiencyinspecificscenarios.1)Largenumericaldatasets:Arraysreducememoryusage.2)Performance-criticaloperations:Arraysofferspeedboostsfortaskslikeappendingorsearching.3)Typesafety:Arraysenforc

Explain how to iterate through the elements of a list and an array.Explain how to iterate through the elements of a list and an array.May 01, 2025 am 12:01 AM

In Python, you can use for loops, enumerate and list comprehensions to traverse lists; in Java, you can use traditional for loops and enhanced for loops to traverse arrays. 1. Python list traversal methods include: for loop, enumerate and list comprehension. 2. Java array traversal methods include: traditional for loop and enhanced for loop.

What is Python Switch Statement?What is Python Switch Statement?Apr 30, 2025 pm 02:08 PM

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

What are Exception Groups in Python?What are Exception Groups in Python?Apr 30, 2025 pm 02:07 PM

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

What are Function Annotations in Python?What are Function Annotations in Python?Apr 30, 2025 pm 02:06 PM

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function