In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Python uses Selenium to automatically watch and learn videos. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First, login
Take the signal and Systems course as an example, enter the URL directly and the login interface appears:
Because the verification code is required for student ID login, select phone login:
Directly find the mobile number input box, password input box and login button in the developer tool, and enter and click:
Import timefrom selenium.webdriver import Chromeweb = Chrome () web.get ('https://mooc2-ans.chaoxing.com/mycourse/stu?courseid=203430340&clazzid=43992529&cpi=93003203&enc=9726840999ffc15f3f441bb5466882e6&t=1637651449831&pageHeader=1')# login phone = web.find_element_by_class_name (' ipt-tel') pwd = web.find_element_by_class_name ('ipt-pwd') login = web.find_element_by_class_name (' btn-big-blue') phone.send_keys ('phone) Number') pwd.send_keys ('password') login.click () time.sleep (2) II. Watch the video on one page.
After the login is successful, it is as follows:
Find all the knowledge points page:
All_no_list = web.find_elements_by_xpath ('/ / span [@ class= "catalog_points_yi"]')
After executing the above code, the error is found, and it is found in the iframe through the developer tool, so you need to enter the iframe first:
# go to iframeframe_content = web.find_element_by_xpath ('/ * [@ id= "frame_content-zj"]') web.switch_to.frame (frame_content) time.sleep (2) # find all unfinished knowledge points page all_no_list = web.find_elements_by_xpath ('/ / span [@ class= "catalog_points_yi"]')
Then go to the first page:
# Jump to the first knowledge point page all_no_list [0] .click () web.switch_to.window (web.window_handles [- 1]) # Jump to the knowledge point interface time.sleep (5)
Find the div of unfinished knowledge points (you also need to enter iframe):
Iframe = web.find_element_by_id ('iframe') # after each refresh, enter the internal iframeweb.switch_to.frame (iframe) # filter to remove the completed knowledge points k_points = web.find_elements_by_css_selector (' div.ans-attach-ct:not (.ans-job-finished)')
Enter the video playback:
Num = len (k_points) for i in range (0, num): k_point = k _ points [I] # number of icons for finding tasks, 1 is knowledge point, 0 is not knowledge point icon_num = len (k_point.find_elements_by_xpath ('. / div [contains (@ class, "ans-job-icon")]') if icon_num = = 0: # filter again Remove div continue # which is not a knowledge point for video playback video_iframe = k_point.find_element_by_xpath ('. / iframe') # video iframe print (video_iframe) time.sleep (2) web.switch_to.frame (video_iframe) # enter video iframe time.sleep (2) web.find_element_by_class_name ('vjs-big-play-button) ') .click () # Click the play button time.sleep (1) web.find_element_by_xpath (' / * [@ id= "video"] / div [5] / div [6] / button'). Click () # mute # play and pause button pause_btn = web.find_element_by_xpath ('/ / button [contains (@ class) "vjs-play-control") and''contains (@ class, "vjs-control") and contains (@ class, "vjs-button")]') while (1): # wait for time.sleep (1) # every second Check whether the video is played if (pause_btn.get_attribute ('title') = = "replay"): # Click and play, that is, the playback status is break print (' video playback is over') # Video playback is finished, exit playback iframe, and then exit the loop Once again find all the knowledge points on the page (k_points) web.switch_to.default_content () print ('exit knowledge point iframe') time.sleep (2)
After testing, it is found that after playing a video, the k_points (that is, the list of knowledge points) has changed, and the elements in the list can no longer be used, so the page needs to be refreshed if it needs to be reacquired.
Watch a video as follows:
# complete all unfinished knowledge points of a page def view_one_page_points (): while (1): iframe = web.find_element_by_id ('iframe') # after each refresh, enter the internal iframe web.switch_to.frame (iframe) # filter Remove the completed knowledge points k_points = web.find_elements_by_css_selector ('div.ans-attach-ct:not (.ans-job-finished)') num = len (k_points) flag = False for i in range (0, num): if I = (num-1): # is the last Indicates the number of flag = True k_point = k _ points [I] # search task icons after the page is scanned. 1 is the knowledge point, 0 is not the knowledge point icon_num = len (k_point.find_elements_by_xpath ('. / div [contains (@ class, "ans-job-icon")]')) if icon_num = = 0: # filter again Remove div continue # which is not a knowledge point for video playback video_iframe = k_point.find_element_by_xpath ('. / iframe') # Video iframe print (video_iframe) time.sleep (2) web.switch_to.frame (video_iframe) # enter video iframe Time.sleep (2) web.find_element_by_class_name ('vjs-big-play-button'). Click () # Click the play button time.sleep (1) web.find_element_by_xpath (' / * [@ id= "video"] / div [5] / div [6] / button'). Click () # Mute # play and Pause button pause_btn = web.find_element_by_xpath ('/ / button [contains (@ class) "vjs-play-control") and''contains (@ class, "vjs-control") and contains (@ class, "vjs-button")]') while (1): # wait for time.sleep (1) # every second Check whether the video is played if (pause_btn.get_attribute ('title') = = "replay"): # Click and play, that is, the playback status is break print (' video playback is over') # Video playback is finished, exit playback iframe, and then exit the loop Once again, find all the knowledge points on the page (k_points) web.switch_to.default_content () print ('exit knowledge point iframe') time.sleep (2) web.refresh () # refresh the page After that, you need to re-enter iframe time.sleep (2) print ('refresh page') break if flag: # the knowledge points on this page have been played and break pass 3. Watch all videos.
I got all the knowledge points on the main page before:
All_no_list = web.find_elements_by_xpath ('/ / span [@ class= "catalog_points_yi"]')
Similar to the k_points in the previous point, it needs to be retrieved every time the all_no_list completes a page, so the code is as follows:
While (1): # go to iframe frame_content = web.find_element_by_xpath ('/ / * [@ id= "frame_content-zj"]') web.switch_to.frame (frame_content) time.sleep (2) # find all unfinished knowledge points page all_no_list = web.find_elements_by_xpath ('/ / span [@ class= "catalog_points_yi"]') List_num = len (all_no_list) # number of pages with knowledge points if list_num = = 0: # pages without knowledge points That is, after browsing break #, jump to the first knowledge point page all_no_list [0] .click () web.switch_to.window (web.window_handles [- 1]) # jump to the knowledge point interface time.sleep (5) # view_one_page_points () # play all the unfinished knowledge point videos of the knowledge point page # print ('complete a knowledge point page.') Web.close () # close the current window # finish the knowledge points on this page and close the current window Select the next knowledge window web.switch_to.window (web.window_handles [0]) # change perspective to the main interface of the course time.sleep (1) # refresh page web.refresh () time.sleep (2) print ('refresh main page') pass 4. Total code import timefrom selenium.webdriver import Chromeweb = Chrome () web.get ('https://mooc2-ans.chaoxing.com/mycourse/stu?courseid=203430340&clazzid=43992529&cpi=93003203&enc=9726840999ffc15f3f441bb5466882e6&t=1637651449831&pageHeader=1')# 1. Log in to phone = web.find_element_by_class_name ('ipt-tel') pwd = web.find_element_by_class_name (' ipt-pwd') login = web.find_element_by_class_name ('btn-big-blue') phone.send_keys (' mobile phone number') pwd.send_keys ('password') login.click () time.sleep (2) # complete all the outstanding knowledge points of a page def view_ One_page_points (): while (1): iframe = web.find_element_by_id ('iframe') # after each refresh All must enter the internal iframe web.switch_to.frame (iframe) # filter Remove the completed knowledge points k_points = web.find_elements_by_css_selector ('div.ans-attach-ct:not (.ans-job-finished)') num = len (k_points) flag = False for i in range (0, num): if I = (num-1): # is the last Indicates the number of flag = True k_point = k _ points [I] # search task icons after the page is scanned. 1 is the knowledge point, 0 is not the knowledge point icon_num = len (k_point.find_elements_by_xpath ('. / div [contains (@ class, "ans-job-icon")]')) if icon_num = = 0: # filter again Remove div continue # which is not a knowledge point for video playback video_iframe = k_point.find_element_by_xpath ('. / iframe') # Video iframe print (video_iframe) time.sleep (2) web.switch_to.frame (video_iframe) # enter video iframe Time.sleep (2) web.find_element_by_class_name ('vjs-big-play-button'). Click () # Click the play button time.sleep (1) web.find_element_by_xpath (' / * [@ id= "video"] / div [5] / div [6] / button'). Click () # Mute # play and Pause button pause_btn = web.find_element_by_xpath ('/ / button [contains (@ class) "vjs-play-control") and''contains (@ class, "vjs-control") and contains (@ class, "vjs-button")]') while (1): # wait for time.sleep (1) # every second Check whether the video is played if (pause_btn.get_attribute ('title') = = "replay"): # Click and play, that is, the playback status is break print (' video playback is over') # Video playback is finished, exit playback iframe, and then exit the loop Once again, find all the knowledge points on the page (k_points) web.switch_to.default_content () print ('exit knowledge point iframe') time.sleep (2) web.refresh () # refresh the page After that, you need to re-enter iframe time.sleep (2) print ('refresh page') break if flag: # after playing the knowledge points on this page, break passwhile (1): # enter iframe frame_content = web.find_element_by_xpath ('/ * [@ id= "frame_content-zj"]') web.switch_to. Frame (frame_content) time.sleep (2) # find all unfinished knowledge points page all_no_list = web.find_elements_by_xpath ('/ / span [@ class= "catalog_points_yi"]') list_num = len (all_no_list) # number of knowledge point pages if list_num = = 0: # No knowledge point page That is, after browsing break #, jump to the first knowledge point page all_no_list [0] .click () web.switch_to.window (web.window_handles [- 1]) # jump to the knowledge point interface time.sleep (5) # view_one_page_points () # play all the unfinished knowledge point videos of the knowledge point page # print ('complete a knowledge point page.') Web.close () # close the current window # finish the knowledge point on this page, close the current window, select the next knowledge point window web.switch_to.window (web.window_handles [0]) # change the perspective to the main interface of the course time.sleep (1) # refresh page web.refresh () time.sleep (2) print ('refresh main page') pass other
Bug:
If the page of the div exists below, the video in it cannot be read.
Thank you for reading! This is the end of this article on "how to use Selenium to automatically watch learning videos in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.