1 #coding= utf-8

2 importre3 importrequests4 importxlrd5

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": Pwd20 }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.text34 title = re.findall(r' (.*?)","', d, re.S)[0]35 returntitle36 exceptException 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 defanswer_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.text81 print(wb_data, index)82 exceptException 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 =093

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 inrange(0, lenOfXls):102 xls =myfile.sheets()[x]103 for i in range(1, xls.nrows):104 title =xls.cell(i, 0).value105 if x != 2:106 answer = xls.cell(i, 7).value107 else:108 answer = xls.cell(i, 2).value109 result[title] =answer110

111 body = ses.get(start_url, headers=headers)112 body.encoding = 'utf-8'

113 wb_data =body.text114 #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'', 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 inexerciseId: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"))

大一python题库刷题训练_python实现合工大试题库自动刷题相关推荐

  1. python判断身份证是否合法的函数_Python开发之身份证验证库id_validator验证身份证号合法性及根据身...

    上个星期,大佬分享了一个验证身份证号合法性的库:id_validator,没空去试着用一下看看,今天有点时间,来试着用下这个库. 1.首先,要安装这个库,windows+R键运行cmd,打开命令行窗口 ...

  2. 计算机二级试题库office选择题,全国计算机二级MSOffice选择题试题库完整

    全国计算机二级MSOffice选择题试题库完整 (46页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 29.9 积分 . . . .(1) 玩帐锌丘楔泅重 ...

  3. 一级计算机等级考试题库,全国计算机等级考试】一级考试试题库1

    <全国计算机等级考试]一级考试试题库1>由会员分享,可在线阅读,更多相关<全国计算机等级考试]一级考试试题库1(9页珍藏版)>请在人人文库网上搜索. 1.2020年全国计算机等 ...

  4. python设计选择题代码源_Python程序的设计试题库完整

    . . . < Python 程序设计>题库 一. 填空题 第一章 基础知识 1 . Python 安装扩展库常用的是 _______ 工具.( pip ) 2 . Python 标准库 ...

  5. 计算机二级36套题解答百度云,全国计算机二级C选择题试题库第36套

    全国计算机二级C选择题题库第36套 1.下列叙述中正确的是 A) 算法复杂度是指算法控制结构的复杂程度 B) 算法复杂度是指设计算法的难度 C) 算法的时间复杂度是指设计算法的工作量 D) 算法的复杂 ...

  6. 计算机组成原理题库带答案详解,计算机组成原理试试题库(含答案解析) -.doc

    Word文档下载可编辑 专业技术资料 计算机组成原理试题 一.单项选择题(从下列各题四个备选答案中选出一个正确答案,并将其代号写在题干前面的括号内.) 1.为了缩短指令中某个地址段的位数,有效的方法是 ...

  7. 邮政社招笔试题库_2016年中国邮政储蓄银行招聘考试笔试题库内容试卷历年真题...

    邮政银行招聘考试笔试复习资料 历年考试真题 建议报考的同学提前做好复习准备 ,考试复习资料可以到"考佳卜资料网" 上面找找, 资料确实不错, 比较有针对性, 资料都是上次参加考试的 ...

  8. python pip安装第三方库老是报错_Python使用pip安装第三方库时报错的解决方案

    报错1: PermissionError: [WinError 5] 报错2: Command... failed with error code 1 in .. 以上两种报错,解决较为简单,主要是权 ...

  9. python中的画布是什么_python详解:turtle库中的画布

    说在前面的话: 很开心上一篇文章能够受到大家受欢迎,同时,这一篇文章也是我写博客有史以来第一篇阅读量破千的文章,很感谢大家的支持,谢谢大家. 今天的这一篇博客同样也是关于turtle库,话不多说,直接 ...

最新文章

  1. MySQL数据库触发器(trigger)
  2. 企消互动广告:网络时代广告活动的创新形式——兼谈杜丽反败为胜对企业的启示...
  3. TCP网络编程中connect()、listen()和accept()三者之间的关系
  4. python中superclass是什么_深度解析并实现python中的super(转载,好文)
  5. python的序列类型及其特点_Fluent Python 笔记——序列类型及其丰富的操作
  6. 阿里P8架构师谈:高并发与多线程的关系、区别、高并发的技术方案
  7. centos7 安装mysql8_CentOS 下 MySQL 8.0 安装部署,超详细!
  8. fprom预测结果内容_生物标志物联合OCT预测ACS患者再发冠脉事件|博“冠”精点...
  9. 在IIS上部署.net core的webapi项目 以及502.5错误的两种解决方法
  10. pandas学习、热力图、子图
  11. Java To CSharp源代码转换
  12. 信息管理学基础(第二版)马费成
  13. qpython3下载不了_qpython3手机版
  14. Fresco按照宽高压缩图片的方法
  15. C++之我见--delete指针
  16. raster包—resample函数
  17. 21天刷题计划之2.1—禁忌雷炎(Java语言描述)
  18. 日志收集之--将Kafka数据导入elasticsearch
  19. Python量化:评估投资组合的收益率和风险
  20. vue print 解决针式打印机打印失败不清晰的问题

热门文章

  1. 对MySQL 进行深入学习是非常必要的
  2. 玩转git-flow工作流-分支解析
  3. Azure 静态 web 应用集成 Azure 函数 API
  4. ASP.NET Core 3.x启动时运行异步任务(一)
  5. 解决 Azure AD 在 Azure Front Door 下登录失败的问题
  6. Abp v2.8.0发布 路线图
  7. 如何使用有序GUID提升数据库读写性能
  8. SonarQube系列二、分析dotnet core/C#代码
  9. .NET Core Love gRPC
  10. 计算机网络原理梳理丨清晰认识 TCP/IP 协议,图解秒懂!