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("請輸入學號\n")
 16 Pwd = input("請輸入密碼\n")
 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\n")
 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\n"))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM