Skip to content

Commit 4a20b34

Browse files
author
xirenbing
committed
规范了查题接口的使用
1 parent 0f40fed commit 4a20b34

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

autochaoxing.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# coding=UTF-8
12
from selenium import webdriver
23
from selenium.webdriver.chrome.options import Options
34
from selenium.webdriver.support.wait import WebDriverWait
@@ -41,6 +42,10 @@ def getvalue(self, _chapter, _section, _subsection, _flag, _end, _pattern):
4142
self.pattern = _pattern
4243

4344

45+
que_type = ['单选题', '多选题', '填空题', '判断题', '简答题', '名词解释题', '论述题', '计算题', '其他',
46+
'分录题', '资料题', '连线题', '', '排序题', '完形填空', '阅读理解', '', '', '口语题', '听力题']
47+
48+
4449
def getlogindata():
4550
with open(r'./logindata.txt', 'r', encoding='utf-8') as f:
4651
return f.readlines()
@@ -59,7 +64,7 @@ def init():
5964
## LOG_ERROR = 2,
6065
## LOG_FATAL = 3
6166
## default is 0
62-
return webdriver.Chrome(options=chrome_options, executable_path=r".\chromedriver")
67+
return webdriver.Chrome(options=chrome_options, executable_path=r"./chromedriver")
6368

6469

6570
def login(driver):
@@ -224,9 +229,11 @@ def get_video_road(text, video_num):
224229
log_fp.write(' video road:' + str(road_lt) + '\n')
225230
return road_lt
226231

227-
#按章节号寻找并播放视频,间隔性检测视频内题目并解答
228-
#如果flags.flag>0 return说明视频时间获取失败, =0则正常
229-
#如果flags.end>0 说明检测到无效章节,manual模式下生效
232+
# 按章节号寻找并播放视频,间隔性检测视频内题目并解答
233+
# 如果flags.flag>0 return说明视频时间获取失败, =0则正常
234+
# 如果flags.end>0 说明检测到无效章节,manual模式下生效
235+
236+
230237
def play_video(driver, menu_url):
231238
wait = WebDriverWait(driver, 20)
232239
action_chains = ActionChains(driver)
@@ -300,11 +307,11 @@ def play_video(driver, menu_url):
300307
iframe = driver.find_element_by_xpath('//iframe')
301308
driver.switch_to.frame(iframe)
302309
sleep(1)
303-
except:#以上各种都找不到视频
310+
except: # 以上各种都找不到视频
304311
print(COLOR.NOTE, ' no videos,continue~', COLOR.END)
305312
log_fp.write(' no videos,continue~\n')
306313
return
307-
314+
308315
# 多视频处理
309316
video_num = driver.execute_script(
310317
"window.scrollTo(0,document.body.scrollHeight);return document.getElementsByClassName('ans-job-icon').length")
@@ -353,7 +360,7 @@ def play_video(driver, menu_url):
353360
# 如果视频任务已完成,访问下一个视频
354361
continue
355362
except:
356-
icon_flag = 0 #有的视频无任务点标识
363+
icon_flag = 0 # 有的视频无任务点标识
357364

358365
# 点击播放并静音
359366
# print(driver.page_source)
@@ -396,11 +403,11 @@ def play_video(driver, menu_url):
396403
# 检测时间获取异常
397404
if total_tm == 0:
398405
log_fp.write(' failed to get total_tm ,will retry this section \n')
399-
print(COLOR.ERR,' failed to get total_tm '+ COLOR.END + ',will retry this section ')
400-
flags.flag += 1 #时间获取异常标志
406+
print(COLOR.ERR, ' failed to get total_tm ' + COLOR.END + ',will retry this section ')
407+
flags.flag += 1 # 时间获取异常标志
401408
return
402409
else:
403-
flags.flag = 0 #时间正常
410+
flags.flag = 0 # 时间正常
404411

405412
# 输出时间信息
406413
nt_index = str(now_tm).find(':')
@@ -510,13 +517,13 @@ def play_video(driver, menu_url):
510517
sleep(10)
511518
need_tm -= 10
512519

513-
print(COLOR.OK+' finish the video: '+COLOR.END+ str(flags.chapter) + '-' + str(flags.section) + '-' + str(v_num))
520+
print(COLOR.OK+' finish the video: '+COLOR.END + str(flags.chapter) + '-' + str(flags.section) + '-' + str(v_num))
514521
log_fp.write(' finish the video: ' + str(flags.chapter) + '-' + str(flags.section) + '-' + str(v_num) + '\n')
515522

516-
flags.end = 0 #只要成功执行到这里就置end为0
523+
flags.end = 0 # 只要成功执行到这里就置end为0
517524

518525

519-
def quiry_ans(question, course_name):
526+
def query_ans(type, question, course_name):
520527
lt = [x for x in range(0, 10)]
521528
# lt.append('(')
522529
lt.extend(['(', ')', '?'])
@@ -528,7 +535,7 @@ def quiry_ans(question, course_name):
528535
goal += urllib.parse.quote(c)
529536
# print(goal)
530537
course = urllib.parse.quote(course_name)
531-
data = {'course': course, 'type': '0', 'option': ''}
538+
data = {'course': course, 'type': str(type)}
532539
headers = {
533540
'Host': 'mooc.forestpolice.org',
534541
'User-Agent':
@@ -567,24 +574,28 @@ def re_process(text, course_name):
567574
regex = re.compile(r'[ \t\n]')
568575
text = regex.sub('', text)
569576
# print(text)
570-
regex = re.compile(r'\u3010[\u4e00-\u9fa5]+?\u3011([\w\W]+?)[ \t\n]*</div>')
577+
regex = re.compile(r'\u3010([\u4e00-\u9fa5]+?)\u3011([\w\W]+?)[ \t\n]*</div>')
571578
que_lt = regex.findall(text) # 问题列表
572579
for i in range(0, len(que_lt)):
573-
que_lt[i] = re.sub(r'<.+?>', '', que_lt[i])
574-
que_lt[i] = re.sub(r'[(](.*?)[)]', '', que_lt[i])
575-
que_lt[i] = re.sub(r'\uff08(.*?)\uff09', '', que_lt[i])
576-
580+
que_lt[i][1] = re.sub(r'<.+?>', '', que_lt[i][1])
581+
que_lt[i][1] = re.sub(r'[(](.*?)[)]', '', que_lt[i][1])
582+
que_lt[i][1] = re.sub(r'\uff08(.*?)\uff09', '', que_lt[i][1])
583+
que_lt[i][0] = re.sub(r' \t\n', '', que_lt[i][0])
577584
# print(que_lt) # 问题
578585
pd_opt = ['正确', '错误', '√', '×', '对', '错', '是', '否', 'T', 'F', 'ri', 'wr']
579586
with open(r'./record.txt', 'a+', encoding="utf-8") as f:
580587
ans_order = [] # 答案序号列表
581588
ans_ul = re.findall(r'<ulclass="[\w\W]+?</ul>', text) # 答案列表
582589
# for item in ans_ul:
583590
for i in range(1, len(ans_ul) + 1):
584-
f.write(que_lt[i - 1])
585-
ans = quiry_ans(que_lt[i - 1], course_name)
591+
f.write(que_lt[i - 1][1])
592+
if que_type.count(que_lt[i-1][0]) == 0:
593+
q_type = 8
594+
else:
595+
q_type = que_type.index(que_lt[i-1][0])
596+
ans = query_ans(q_type, que_lt[i - 1][1], course_name)
586597
# ans为0,未获取到答案
587-
log_fp.write(' go to ' + str(que_lt[i - 1]) + ' get ' + str(ans) + '\n')
598+
log_fp.write(' go to ' + str(que_lt[i - 1][1]) + ' get ' + str(ans) + '\n')
588599
if ans == 0:
589600
ans_order.append([1]) # 服务器异常,默认选1
590601
continue

0 commit comments

Comments
 (0)