Python 3 uses Selenium 4 Cloud School to automatically brush video scripts

» Python » Python 3 uses Selenium 4 Cloud School to automatically brush video scripts

Preface: Selenium is a Python browser automation library. The latest version is 4, which has changed a lot from the previous version

Version requirements

Python 3.8 or above

Drive download

Because Edge is the default browser, it is recommended to use this first, but I developed and tested Firefox's
Chrome: https://chromedriver.chromium.org/downloads
Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox: https://github.com/mozilla/geckodriver/releases
Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/

Drive use

Add PATH after downloading the above driver software, and normal output can be executed in the command line.

Software script

 #!/ usr/bin/env python # -*- encoding: utf-8 -*- """ @File: run.py @Description:       : @Date     :2023/10/01 13:28:46 @Author      :yunluo @version      :1.0 """ import contextlib import time from ctypes import windll from loguru import logger from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys def popup(title: str,  msg: str, info_type: str): msg_dict = {"error": 16, "question": 32, "warn": 48, "info": 64} return windll.user32.MessageBoxW(None, msg, title, msg_dict.get(info_type)) def get_list_url(browser): "" "Log in manually and get the directory page" "" try: list_url = "https://******.yunxuetang.cn/kng/#/list" browser.get(list_url) Popup ("system notification", "Please use manual login  n  nPlease click", "info" after login) browser.get(list_url) time.sleep(2) browser.refresh() time.sleep(2) return browser.current_url except Exception as e: logger.error(e) def back_task_list(browser, new_list_url): "" "Return to the task list, refresh, and wait a few seconds" "" Courl_sleep (browser, new_list_url, "click to return, wait 2 seconds", 5) browser.refresh() time.sleep(2) def gourl_sleep(browser,  new_list_url, arg2, arg3): "" "Open the link and wait a few seconds" "" browser.get(new_list_url) logger.info(arg2) time.sleep(arg3) def click_sleep(arg0, arg1, arg2): "" "Click on the element and wait a few seconds" " arg0.click() logger.info(arg1) time.sleep(arg2) def check_player_status(browser): old_task_slot = "" #Loop to handle some exceptions while browser.find_element(By.CSS_SELECTOR, "span.opacity8").text.strip() not in ( "Learning completed", "Learning completed", ): #Check whether it is completed check_task_status = browser.find_element(By.CSS_SELECTOR, "span.opacity8").text if jixu_btn := find_exsit_element( browser, ".yxtf-dialog__body button.yxtf-button--primary", ): If jixu_btn.text=="Continue learning": time.sleep(3) Logger.info ("Find Continue Learning Pop up") Click_sleep (jixu_btn, "click to continue learning pop-up", 5) Logger.info ("Click to continue learning pop-up") #Get Remaining Time if task_slot_obj := find_exsit_element(browser, ".yxt-color-warning"): task_slot = task_slot_obj.text Logger. info (f "Check learning status: {check_task_status}, remaining time: {task_slot}") #One situation is that the video is finished, and the progress is still a little poor. Here is the solution to this problem if old_task_slot == task_slot: Logger.info ("If the video is stopped, refresh it") ActionChains(browser).move_by_offset(200, 200).click().perform() old_task_slot = task_slot else: Logger. info (f "Check the learning status: {check_task_status}") time.sleep(30) #End of player interface monitoring def for_page_course(browser, new_list_url): """ Open all courses on the current page """ for k in range(16): try: child = k + 1 css_selector = f"ul.kng-list li:nth-child({child})" task = browser.find_element(By.CSS_SELECTOR, css_selector) #Logger. info (f "circular task list: {task}") list_task_obj = task.text.strip().split("\n") list_task_title = list_task_obj[-4] list_task_status = list_task_obj[-5] If "Learned" in list_task_status: continue Click_sleep (task, f "the course has not been learned, click to enter, {list_task_title}", 2) #Get course status if task_status_obj := find_exsit_element(browser, "span.opacity8"): task_status = task_status_obj.text Logger. info (f "status: {task_status}") #Here is a 2-fold acceleration switch, which can be achieved by changing the link new_current_url = f"{browser.current_url}&rate=2" browser.get(new_current_url) time.sleep(1) browser.refresh() time.sleep(3) #If you haven't learned, click the Start Play button if start_btn := find_exsit_element( browser, ".yxtulcdsdk-flex-center-center button.yxtf-button--primary", ): Click_sleep (start_btn, "Click to start playing, start playing", 3) ActionChains(browser).move_by_offset(200, 200).click().perform() time.sleep(3) #Check the playback check_player_status(browser) except Exception as e: logger.error(e) #After learning, start to return time.sleep(2) back_task_list(browser, new_list_url) def find_exsit_element(browser, selecter): "" "Found element, does not exist, returns None" "" with contextlib.suppress(NoSuchElementException): return browser.find_element( By.CSS_SELECTOR, selecter, ) def run(): """ function """ try: logger.add( f"run_{time.strftime('%Y%m%d')}.log", rotation="2 MB", encoding="utf-8", enqueue=True, ) #Firefox browser driver browser = webdriver.Firefox() #Edge browser driver # browser = webdriver. Edge() browser.maximize_window() list_url = get_list_url(browser) if not list_url: return #Current page+2 is video classification new_list_url = f" {list_url}2 " # new_list_url = list_url Courl_sleep (browser, new_list_url, "Re entering the video page", 2) if next_btn := find_exsit_element(browser, ".driver-next-btn"): next_btn.click() Logger. info ("Click the boot button") browser.refresh() time.sleep(3) #Start all cycles, and keep going as long as the next page exists while find_exsit_element(browser, ".btn-next") is not None: try: #Get Page Course task_page_number = browser.find_element( By.CSS_SELECTOR,  "ul.yxtf-pager li.active" ).text Logger. info (f "current page number: {task_page_number}") #Cycle through all courses on the current page for_page_course(browser, new_list_url) browser.find_element(By.TAG_NAME, "body").send_keys(Keys.END) time.sleep(2) if nav_pages := find_exsit_element(browser, ".btn-next"): Click_sleep (nav_pages, "click the next page", 2) except Exception as e: logger.error(e) continue except Exception as e: logger.info(e) if __name__ == "__main__": #Start running run()

Code interpretation

Because there is no problem to end the login, a pop-up window appears after running, and you need to log in manually. After logging in, click the pop-up button, and then the automatic operation will be started
 Python 3 uses Selenium 4 Cloud School to automatically brush video scripts - Geek Park

--End--

-- 完 --

Post reply

Your email address will not be disclosed. Required items have been used * tagging