1- # coding=UTF-8
1+ # coding=utf-8
2+ ##
3+ # brief
4+ # details
5+ # author Luoofan
6+ # date 2020-03-15 15:40:43
7+ # FilePath\autochaoxing.py
8+ #
29from selenium import webdriver
310from selenium .webdriver .chrome .options import Options
411from selenium .webdriver .support .wait import WebDriverWait
@@ -27,19 +34,21 @@ class color:
2734
2835class FLAG :
2936 pattern = 0
30- flag = 0
37+ flag = 0 # 视频时间获取异常标志
38+ flag2 = 0 # 播放视频异常标志
3139 end = 0
3240 chapter = 0
3341 section = 0
3442 subsection = 0
3543
36- def getvalue (self , _chapter , _section , _subsection , _flag , _end , _pattern ):
44+ def getvalue (self , _chapter , _section , _subsection , _flag , _end , _pattern , _flag2 = 0 ):
3745 self .chapter = _chapter
3846 self .section = _section
3947 self .subsection = _subsection
4048 self .flag = _flag
4149 self .end = _end
4250 self .pattern = _pattern
51+ self .flag2 = _flag2
4352
4453
4554que_type = ['单选题' , '多选题' , '填空题' , '判断题' , '简答题' , '名词解释题' , '论述题' , '计算题' , '其他' ,
@@ -177,30 +186,34 @@ def get_chapter_section(driver, menu_url):
177186 WebDriverWait (driver , 30 ).until (EC .presence_of_element_located ((By .XPATH , '//div[@class="timeline"]' )))
178187 lt = driver .find_element_by_xpath ('//div[@class="timeline"]' )
179188 sleep (3 )
180-
189+
181190 ch_se_lt = []
182191 end = 0
183192 for i in range (1 , 50 ):
184193 if end == 2 :
185194 break
186195 try :
187196 for j in range (1 , 50 ):
188- # print(i,j)
189197 icon = lt .find_element_by_xpath ('//div[' + str (i ) + ']/div[' + str (j ) + ']' + '/h3/span[@class="icon"]' )
190198 end = 0
191- name = lt .find_element_by_xpath (
192- '//div[' + str (i ) + ']/div[' + str (j ) + ']' + '/h3/span[@class="articlename"]/a' ).get_attribute ('title' )
193- if name in ["问卷调查" , "阅读" ]:
194- continue
195- # if 'openlock' not in icon.get_attribute('innerHTML'):
199+ #print(str(i)+' '+str(j)+' '+re.sub(r'[ \t\n]+','',icon.get_attribute('innerHTML')))
200+
201+ try :
202+ name = lt .find_element_by_xpath (
203+ '//div[' + str (i ) + ']/div[' + str (j ) + ']' + '/h3/span[@class="articlename"]/a' ).get_attribute ('title' )
204+ if name in ["问卷调查" , "阅读" ]:
205+ continue
206+ except :
207+ pass
196208 try :
197209 # 寻找三级标题
198210 three_levels = lt .find_element_by_xpath (
199211 '//div[' + str (i ) + ']/div[' + str (j ) + ']' + '/div[@class="levelthree"]' )
200212 ch_se_lt .extend ([* map (lambda x :[i , j , x ], bs4_3rd_titles (three_levels .get_attribute ('innerHTML' )))])
201213 except :
202214 # 找不到三级标题
203- if 'orange' in icon .get_attribute ('innerHTML' ):
215+ icon_html = icon .get_attribute ('innerHTML' )
216+ if 'orange' in icon_html or "display:inline-block;" in icon_html :
204217 ch_se_lt .append ([i , j ])
205218 except :
206219 end += 1
@@ -235,7 +248,7 @@ def get_video_road(text, video_num):
235248
236249
237250def play_video (driver , menu_url ):
238- wait = WebDriverWait (driver , 20 )
251+ wait = WebDriverWait (driver , 60 )
239252 action_chains = ActionChains (driver )
240253 # 进入课程主页进行选节播放
241254 driver .get (menu_url )
@@ -366,27 +379,36 @@ def play_video(driver, menu_url):
366379 # print(driver.page_source)
367380 #wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="ans-cc"]/p['+str(p_index[v_num-1])+']/div/iframe')))
368381 #iframe = driver.find_element_by_xpath('//div[@class="ans-cc"]/p['+str(p_index[v_num-1])+']/div/iframe')
369- wait .until (EC .presence_of_element_located ((By .XPATH , first_road + video_road [v_num - 1 ] + '/iframe' )))
370- iframe = driver .find_element_by_xpath (first_road + video_road [v_num - 1 ] + '/iframe' )
371- driver .switch_to .frame (iframe )
382+ try :
383+ wait .until (EC .presence_of_element_located ((By .XPATH , first_road + video_road [v_num - 1 ] + '/iframe' )))
384+ iframe = driver .find_element_by_xpath (first_road + video_road [v_num - 1 ] + '/iframe' )
385+ driver .switch_to .frame (iframe )
372386
373- wait .until (EC .presence_of_element_located (
374- (By .XPATH , '//div[@id="reader"]/div/button[@class="vjs-big-play-button"]' )))
375- bt = driver .find_element_by_xpath ('//div[@id="reader"]/div/button[@class="vjs-big-play-button"]' )
376- action_chains .move_to_element_with_offset (bt , 0 , 0 )
377- # action_chains.click(bt)
378- sleep (5 )
379- bt .click () # 点击播放
380- print (COLOR .OK + ' start play ' + COLOR .END )
381- log_fp .write (' start play \n ' )
387+ wait .until (EC .presence_of_element_located (
388+ (By .XPATH , '//div[@id="reader"]/div/button[@class="vjs-big-play-button"]' )))
389+ bt = driver .find_element_by_xpath ('//div[@id="reader"]/div/button[@class="vjs-big-play-button"]' )
390+ action_chains .move_to_element_with_offset (bt , 0 , 0 )
391+ # action_chains.click(bt)
392+ sleep (5 )
393+ bt .click () # 点击播放
394+ print (COLOR .OK + ' start play ' + COLOR .END )
395+ log_fp .write (' start play \n ' )
396+ flags .flag2 = 0
397+ except :
398+ # 找的到视频但无法播放
399+ log_fp .write (' failed to start,Retry \n ' )
400+ print (COLOR .ERR , ' failed to start ' + COLOR .END + ',will retry this section ' )
401+ flags .flag2 = 1 # 视频获取异常标志
402+ return
382403
383- wait .until (EC .presence_of_element_located (
384- (By .XPATH , '//button[@class="vjs-mute-control vjs-control vjs-button vjs-vol-3"]' )))
385- volumn = driver .find_element_by_xpath ('//button[@class="vjs-mute-control vjs-control vjs-button vjs-vol-3"]' )
386- action_chains .move_to_element (volumn )
387404 try :
405+ wait .until (EC .presence_of_element_located (
406+ (By .XPATH , '//button[@class="vjs-mute-control vjs-control vjs-button vjs-vol-3"]' )))
407+ volumn = driver .find_element_by_xpath (
408+ '//button[@class="vjs-mute-control vjs-control vjs-button vjs-vol-3"]' )
409+ action_chains .move_to_element (volumn )
388410 volumn .click () # 点击静音
389- except :
411+ except : # 无法定位静音
390412 sleep (1 )
391413
392414 # 获取时间信息
@@ -577,6 +599,7 @@ def re_process(text, course_name):
577599 regex = re .compile (r'\u3010([\u4e00-\u9fa5]+?)\u3011([\w\W]+?)[ \t\n]*</div>' )
578600 que_lt = regex .findall (text ) # 问题列表
579601 for i in range (0 , len (que_lt )):
602+ que_lt [i ] = list (que_lt [i ])
580603 que_lt [i ][1 ] = re .sub (r'<.+?>' , '' , que_lt [i ][1 ])
581604 que_lt [i ][1 ] = re .sub (r'[(](.*?)[)]' , '' , que_lt [i ][1 ])
582605 que_lt [i ][1 ] = re .sub (r'\uff08(.*?)\uff09' , '' , que_lt [i ][1 ])
@@ -649,13 +672,14 @@ def ans_question(driver, course_name):
649672 #print(6, end=" ")
650673
651674 # 进入答题界面
652- wait = WebDriverWait (driver , 20 )
675+ wait = WebDriverWait (driver , 60 )
653676 wait .until (EC .presence_of_element_located ((By .XPATH , '//iframe[1]' )))
654677 iframe = driver .find_element_by_xpath ('//iframe[1]' )
655678 driver .switch_to .frame (iframe )
656679 #print(7, end=" ")
657680
658681 # 检测是否已完成
682+ sleep (10 )
659683 wait .until (EC .presence_of_element_located ((By .XPATH , '//div[@class="ans-cc"]' )))
660684 ans_cc = driver .find_element_by_xpath ('//div[@class="ans-cc"]' )
661685 action_chains .move_to_element (ans_cc )
@@ -747,13 +771,13 @@ def ans_question(driver, course_name):
747771 action_chains .move_to_element (bn )
748772 try :
749773 bn .click ()
750- print (COLOR .OK , 'questions of the section is already finished! continue~' , COLOR .END )
774+ print (COLOR .OK , 'questions of the section is finished! continue~' , COLOR .END )
751775 log_fp .write (' finish the questions ' + '\n ' )
752776 except :
753777 print (COLOR .ERR , " 提交失败!" , COLOR .END )
754778 log_fp .write (" 提交失败!" + '\n ' )
755779 return str (flags .chapter ) + '-' + str (flags .section )
756- # sleep(1 )
780+ sleep (5 )
757781 # driver.switch_to_alert().accept()
758782
759783 return 0
@@ -768,18 +792,25 @@ def perform(driver, menu_url, course_name):
768792 print (COLOR .WARN , '未完成任务章节:' , COLOR .END , str (ch_se_lt ))
769793 log_fp .write ('未完成任务章节:' + str (ch_se_lt ) + '\n ' )
770794 error_lt = []
795+ last_time = time .time ()- 300
771796 if pattern == 1 :
772797 chapter = eval (input (COLOR .NOTE + "please select which chapter:" + COLOR .END ))
773798 section = eval (input (COLOR .NOTE + "please select which section:" + COLOR .END ))
774799 subsection = eval (input (COLOR .NOTE + "please select which subsection(if not input 0):" + COLOR .END ))
775800 flags .getvalue (chapter , section , subsection , 0 , 0 , pattern )
776801 while 1 :
777802 play_video (driver , menu_url )
778- if flags .flag > 0 :
803+ if flags .flag > 0 or flags . flag2 > 0 :
779804 print (COLOR .ERR , 'unfinished! Retry…' , COLOR .END )
780805 continue
781806 elif flags .end > 0 :
807+ flags .section += 1
782808 continue
809+ now_time = time .time ()
810+ #print(now_time-last_time)
811+ if now_time - last_time < 300 :
812+ sleep (300 - (now_time - last_time ))
813+ last_time = time .time ()
783814 if flags .end == 2 :
784815 print (COLOR .OK , 'finish the lesson! quit! ' , COLOR .END )
785816 err_section = ans_question (driver , course_name )
@@ -797,10 +828,17 @@ def perform(driver, menu_url, course_name):
797828 flags .subsection = ch_se [2 ] if len (ch_se ) == 3 else 0
798829 while 1 :
799830 play_video (driver , menu_url )
800- if flags .flag > 0 :
831+ if flags .flag > 0 or flags . flag2 > 0 :
801832 print (COLOR .ERR , 'unfinished! Retry…' , COLOR .END )
802833 else :
803834 break
835+
836+ now_time = time .time ()
837+ #print(now_time-last_time)
838+ if now_time - last_time < 300 :
839+ sleep (300 - (now_time - last_time ))
840+ last_time = time .time ()
841+
804842 err_section = ans_question (driver , course_name )
805843 if err_section != 0 :
806844 print (COLOR .ERR , 'unfinished!' , COLOR .END )
@@ -815,7 +853,7 @@ def perform(driver, menu_url, course_name):
815853
816854def main ():
817855 driver = init ()
818- print (COLOR .DISPLAY , 'Lauching…' , COLOR .END )
856+ print (' \n \n ' + COLOR .DISPLAY , 'Lauching…' , COLOR .END )
819857 login (driver )
820858 # print(driver.current_url)
821859 ret = choose_course (driver )
0 commit comments