zoukankan      html  css  js  c++  java
  • python实现合工大试题库自动刷题

    具体代码参见:https://github.com/Jie-OY/python-funny

    语言: python 3.5 (2.7也行)

    库:requests, re, xlrd

      1 # coding= utf-8
      2 import re
      3 import requests
      4 import xlrd
      5 
      6 save_url = "http://tkkc.hfut.edu.cn/student/exam/manageExam.do?1479131327464&method=saveAnswer"
      7 # index用于提示题目序号
      8 index = 1
      9 headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/41.0",
     10            "Host": "tkkc.hfut.edu.cn",
     11            "X-Requested-With": "XMLHttpRequest",
     12            }
     13 
     14 ses = requests.session()
     15 ID = input("请输入学号
    ")
     16 Pwd = input("请输入密码
    ")
     17 logInfo = {
     18     "logname": ID,
     19     "password": Pwd
     20 }
     21 login_url = "http://tkkc.hfut.edu.cn/login.do?"
     22 res = ses.post(login_url, data=logInfo, headers=headers)
     23 
     24 # 用于存放excel中question,answer键值对的字典
     25 result = dict()
     26 
     27 
     28 # retries默认为2,表示尝试次数。以防某种原因,某次连接失败
     29 def craw(url, retries=2):
     30     try:
     31         b = ses.post(url, headers=headers)
     32         b.encoding = 'utf-8'
     33         d = b.text
     34         title = re.findall(r' (.*?)","', d, re.S)[0]
     35         return title
     36     except Exception as e:
     37         print(e)
     38         if retries > 0:
     39             return craw(url, retries=retries - 1)
     40         else:
     41             print("get failed", index)
     42             return ''
     43 
     44 
     45 # 从字典中根据题目找到并返回答案
     46 def answer_func(t):
     47     return result.get(title, "Not Found")
     48 
     49 
     50 # 将找到的答案提交给服务器
     51 def submit(ans, id, id2, id3, id4, index, retries=2):
     52     dx = ["false", "false", "false", "false", "false"]
     53     try:
     54         if ans.find('A') != -1:
     55             dx[0] = "true"
     56         if ans.find('B') != -1:
     57             dx[1] = "true"
     58         if ans.find('C') != -1:
     59             dx[2] = "true"
     60         if ans.find('D') != -1:
     61             dx[3] = "true"
     62         if ans.find('E') != -1:
     63             dx[4] = "true"
     64         if ans.find('正确') != -1:
     65             ans = "A"
     66         if ans.find('错误') != -1:
     67             ans = "B"
     68         data2 = {"examReplyId": id3,
     69                  "examStudentExerciseId": id2,
     70                  "exerciseId": id,
     71                  "examId": id4,
     72                  "DXanswer": ans,
     73                  "PDanswer": ans,
     74                  "DuoXanswerA": dx[0],
     75                  "DuoXanswerB": dx[1],
     76                  "DuoXanswerC": dx[2],
     77                  "DuoXanswerD": dx[3],
     78                  "DuoXanswerE": dx[4]}
     79         body = ses.post(save_url, data=data2, headers=headers)
     80         wb_data = body.text
     81         print(wb_data, index)
     82     except Exception as e:
     83         print(e)
     84         if retries > 0:
     85             return submit(ans, id, id2, id3, id4, index, retries=retries - 1)
     86         else:
     87             print("get failed", index)
     88             return ''
     89 
     90 
     91 # 此变量用于判断用户是否要继续刷课
     92 finished = 0
     93 
     94 while finished == 0:
     95     start_url = input("请输入测试页面URL
    ")
     96     
     97     myfile = xlrd.open_workbook('exercise.xls')
     98     lenOfXls = len(myfile.sheets())
     99 
    100     # 读取XLS中的题目和答案,存进字典(将这段程序放在这,是因为当用户有多门试题库时,刷完一门,切换到另一门时,不用关闭程序只需切换题库Excel即可)
    101     for x in range(0, lenOfXls):
    102         xls = myfile.sheets()[x]
    103         for i in range(1, xls.nrows):
    104             title = xls.cell(i, 0).value
    105             if x != 2:
    106                 answer = xls.cell(i, 7).value
    107             else:
    108                 answer = xls.cell(i, 2).value
    109             result[title] = answer
    110 
    111     body = ses.get(start_url, headers=headers)
    112     body.encoding = 'utf-8'
    113     wb_data = body.text
    114     # print(wb_data)
    115 
    116     urlId = re.findall(r'do?(.*?)&method', start_url, re.S)[0]
    117 
    118     eval = re.findall(r'eval(.*?)]);', wb_data, re.S)[0]
    119 
    120     examReplyId = re.findall(r'examReplyId=(.*?)&examId', wb_data, re.S)[0]
    121 
    122     examId = re.findall(r'<input type="hidden" name="examId" id="examId" value="(.*?)" />', wb_data, re.S)[0]
    123 
    124     exerciseId = re.findall(r'exerciseId":(.*?),', eval, re.S)
    125 
    126     examSEId = re.findall(r'examStudentExerciseId":(.*?),', eval, re.S)
    127 
    128     examStudentExerciseId = re.findall(r'"examStudentExerciseId":(.*?),"exerciseId"',
    129                                        wb_data, re.S)[0]
    130 
    131     print(examStudentExerciseId)
    132     examStudentExerciseId = int(examStudentExerciseId)
    133 
    134     # id对应exerciseID,id2对应examStudetExerciseId
    135     for id in exerciseId:
    136         next_url = r"http://tkkc.hfut.edu.cn/student/exam/manageExam.do?%s&method=getExerciseInfo&examReplyId=%s&exerciseId=%s&examStudentExerciseId=%d" % (
    137             urlId, examReplyId, id, examStudentExerciseId)
    138         title = craw(next_url)
    139         ans = answer_func(title)
    140         submit(ans, id, examStudentExerciseId, examReplyId, examId, index)
    141         # time.sleep(1)
    142         index += 1
    143         examStudentExerciseId = examStudentExerciseId + 1
    144     # input函数获取到的为字符串,所以进行Type conversion
    145     finished = int(input("继续请输入0,退出请输入1
    "))
  • 相关阅读:
    Spring jdbcTemplat 写入BLOB数据为空
    Android强制弹出,隐藏输入法.
    Android NDK开发篇(五):Java与原生代码通信(数据操作)
    AIX加入�能够telnet远程连接账号方法
    index of rmvb mp3 rm突破站点入口下载
    C++“窗体”程序设计启蒙(之二)
    C++ Primer 学习笔记_72_面向对象编程 --句柄类与继承[续]
    (a*b)%c 小的技巧
    javascript(arguments)
    Cocos2dx 小技巧(十四)ScrollView实现缩放效果
  • 原文地址:https://www.cnblogs.com/godoyj/p/6069273.html
Copyright © 2011-2022 走看看