自动化播放视频 Created 2021-12-09 | Updated 2022-05-22
| Post Views:
本次我采用Python+selenium写了一个自动播放视频的脚本。
从账号密码登录,切换一个个window,再到切换一层层frame,最终点击播放按钮的过程。
导入包
1 2 3 4 5 6 7 from selenium import webdriver from selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWaitimport time
将webdriver静音
1 2 3 4 options = webdriver.ChromeOptions() options.add_argument("--mute-audio" ) browser = webdriver.Chrome(options=options)
模拟浏览器,顺便进行反屏蔽
1 2 3 4 5 browser = webdriver.Chrome() browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument' , { 'source' : 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})' })
获取url
1 2 url = "http://passport2.chaoxing.com/login?fid=&newversion=true&refer=http%3A%2F%2Fi.chaoxing.com" browser.get(url)
输入账号密码,再登录
1 2 3 4 5 6 7 8 inp='your_account' inp2='your_password' username=browser.find_element(By.XPATH,'//*[@id="phone"]' ) password=browser.find_element(By.XPATH,'//*[@id="pwd"]' ) username.send_keys(inp) password.send_keys(inp2) login=browser.find_element(By.XPATH,'//*[@id="loginBtn"]' ) login.click()
此时,网页中会有一个iframe,导致selenium无法正常读取iframe里的html代码,这里用到了selenium的switch_to.frame()的方法
如果click()某个元素之后,弹出一个新窗口,那么selenium同样无法获取新窗口的元素,这里就用到了switch_to.window()的方法
以下方法就是关闭第一个窗口切换到第二个窗口
1 2 3 4 5 6 7 handles = browser.window_handles print (handles)for handle in handles: if handle != browser.current_window_handle: print ('switch to second window' , handle) browser.close() browser.switch_to.window(handle)
完整代码1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 from selenium import webdriver from selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWaitimport timechrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--mute-audio" ) browser = webdriver.Chrome(options=chrome_options) browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument' , { 'source' : 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})' }) wait=WebDriverWait(browser, 10 ) url = "http://passport2.chaoxing.com/login?fid=&newversion=true&refer=http%3A%2F%2Fi.chaoxing.com" browser.get(url) browser.maximize_window() inp='15975027211' inp2='@Qq2670387848' username=browser.find_element(By.XPATH,'//*[@id="phone"]' ) password=browser.find_element(By.XPATH,'//*[@id="pwd"]' ) username.send_keys(inp) password.send_keys(inp2) login=browser.find_element(By.XPATH,'//*[@id="loginBtn"]' ) login.click() time.sleep(2 ) course=browser.find_element(By.XPATH,'//*[@id="first57734"]' ) course.click() browser.switch_to.frame("frame_content" ) course1=wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="c_220056940"]/div[2]/h3/a/span' ))) course1.click() handles = browser.window_handles print (handles)for handle in handles: if handle != browser.current_window_handle: print ('switch to second window' , handle) browser.close() browser.switch_to.window(handle) browser.switch_to.frame("frame_content-zj" ) item=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#fanyaChapter > div > div.chapter_body.xs_table > div.chapter_td > div:nth-child(2) > div.catalog_level > ul > li:nth-child(1) > div > div > div.catalog_name' ))) item.click() while 1 : handles = browser.window_handles print (handles) for handle in handles: if handle != browser.current_window_handle: print ('switch to second window' , handle) browser.close() browser.switch_to.window(handle) time.sleep(2 ) browser.switch_to.frame("iframe" ) element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ext-gen1044"]/iframe' ))) browser.switch_to.frame(element) play=wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="video"]/button' ))) play.click() time.sleep(180 ) browser.refresh()
例二 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 import timefrom selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver import ActionChainsdef create_web_driver (url ): options = webdriver.ChromeOptions() options.add_argument("--mute-audio" ) browser = webdriver.Chrome(options=options) browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument' , { 'source' : 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})' }) browser.get(url) browser.maximize_window() return browser def login (browser,user_name,password ): wait = WebDriverWait(browser, 10 ) login_button = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="y-user-account"]/span/a[2]' ))) login_button.click() wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="account-txt"]' ))).send_keys(user_name) browser.find_element(By.XPATH, '//*[@id="password-txt"]' ).send_keys(password) browser.find_element(By.XPATH,'//*[@id="login-btn"]' ).click() def action (browser ): wait = WebDriverWait(browser, 10 ) a=wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="YOOC_MENU"]' ))) a.click() handles = browser.window_handles print (handles) for handle in handles: if handle != browser.current_window_handle: print ('switch to second window' , handle) browser.close() browser.switch_to.window(handle) time.sleep(3 ) hover_element = browser.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[1]' ) ActionChains(browser).move_to_element(hover_element).perform() time.sleep(3 ) browser.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[2]/ul/li[2]/a' ).click() browser.find_element(By.XPATH, '/html/body/div[2]/div[2]/table/tbody/tr/td/table[2]/tbody/tr/td[2]/a/h1' ).click() browser.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/div/a' ).click() i = 0 while 1 : browser.find_element(By.XPATH, f'/html/body/div[3]/div/div[2]/ul/li[{26 +i} ]/a/h4' ).click() time.sleep(8 ) hover_element = browser.find_element(By.XPATH, '//*[@id="video"]' ) ActionChains(browser).move_to_element(hover_element).perform() time.sleep(3 ) browser.find_element(By.XPATH, '//*[@id="video"]' ).click() time.sleep(20 *60 ) i=i+1 print (i) if __name__ == '__main__' : user_name='' password='' brower=create_web_driver("https://www.yiban.cn/" ) login(brower,user_name,password) action(brower)