selenium网页自动化之学某通自动看章节视频 您所在的位置:网站首页 python脚本自动播放视频 selenium网页自动化之学某通自动看章节视频

selenium网页自动化之学某通自动看章节视频

2024-06-25 05:06| 来源: 网络整理| 查看: 265

目录

 

 

模块导入

加载浏览器驱动

创建txt文件

学习通登录账号

进入课程选择

进入课程章节

章节学习

自动做视频章节的习题

检测每一章节是否有多个视频

总代码

 

模块导入 import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.chrome.options import Options 加载浏览器驱动

 这里用的是Chrome浏览器,需要下载Chrome浏览器的驱动(这里就不过多说明了)这里添加了两个功能

浏览器声音浏览器隐藏(这个好像后面看视频的时候要报错) music = input('浏览器静音是否启动Y/N,默认为不启动') head = input('浏览器是否启用无窗口模式Y/N,默认为不启动') chrome_options = Options() if music == 'Y': chrome_options.add_argument('--mute-audio') # 浏览器静音 if head == 'Y': chrome_options.add_argument('--headless') # 浏览器隐藏 driver = webdriver.Chrome(options=chrome_options) 创建txt文件

创建一个文件以后以便进行继续观看章节,没有文件就创建一个文件,就不用退出程序再进行观看时从第一章节开始看

while True: video_num=input('输入已看的视频数/不输入表示延续上一次内容:') if video_num=='': break try: video_num=int(video_num) with open("text.text", 'w', encoding="UTF-8") as f: f.write('已完成章节:'+str(video_num)) break except: print('请输入整数') with open("text.text",encoding="UTF-8") as f: b=int(str(f.read()).split("已完成章节:")[1]) a=b a1=b c = 0 print('正在加载中请稍等......') 学习通登录账号

 实现输入账号和密码,密码错误重新输入。这里用的是学习通的这个地址https://passport2.chaoxing.com/login?fid=&refer=

driver.get('https://passport2.chaoxing.com/login?fid=&refer=') phone = driver.find_element(By.CLASS_NAME, 'ipt-tel') pwd = driver.find_element(By.CLASS_NAME, 'ipt-pwd') login = driver.find_element(By.CLASS_NAME, 'btn-big-blue') while True: loginname = input("请输入用户名:") loginpassword = input("请输入密码:") phone.send_keys(loginname) pwd.send_keys(loginpassword) login.click()#点击登录 time.sleep(2) try: loginerr=driver.find_element(By.ID,'err-txt').text if loginerr=='手机号或密码错误': print(loginerr+'请重新输入') phone.clear() pwd.clear() except: print('登录成功') break 进入课程选择

这里需要进入iframe找到对应的课程并输出选择进入课程,注意进入课程后要刷新并且退出之前的iframe

time.sleep(2) driver.switch_to.frame("frame_content") time.sleep(2) tab=driver.find_elements(By.CLASS_NAME,'color1') chapters=0 for i in tab: chapters=chapters+1 print('第'+str(chapters)+'章《'+i.text+'》') choose=int(input('请输入学习第几章:')) choose=choose-1 chooselink=tab[choose].get_attribute('href')#获取对应章节的链接 driver.get(chooselink) driver.refresh() driver.switch_to.default_content() 进入课程章节

这里也是花了很久的时间,一样的需要进入iframe,最开始获取一个离线的数据,还有在线的实时更新的数据。他们进行对比。因为我是获取的知识点作为数据,所以涉及到除了视频的知识点还有习题的知识点。所以必须进行比对分析(注意变量b)再进入下一章节。

while True: WebDriverWait(driver, 10) # 等待响应 if a > a1: driver.refresh() time.sleep(2) driver.switch_to.default_content() WebDriverWait(driver,10) driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/div[1]/div/ul[1]/li[2]/a').click() frame_content = driver.find_element(By.XPATH, '//*[@id="frame_content-zj"]')#进入iframe driver.switch_to.frame(frame_content) all_no_list = driver.find_elements(By.XPATH, '//span[@class="catalog_points_yi"]')#查找所有未完成的知识点页面 inlist_num = len(all_no_list) # 在线数据(每次循环都会更新) if a == a1: unlist_num = len(all_no_list) # 离线数据(只是第一次循环更新) end_num = len(all_no_list)-b # 现有章节 allin_num = len(all_no_list) # 总章节 allin1 = allin_num - a print('总章节:', allin_num, '节') print('现有章节章节:', end_num, '节') print('已完成章节:', a, '节') print('剩余章节:', allin1, '节') print('----------------------------------------------------------------------') if end_num == 0: # 比对现有章节,没有知识点页面,即全部刷完 with open("text.text", 'w', encoding="UTF-8") as f: f.write('已完成章节:0') print('知识点已全部完成') break if unlist_num == inlist_num:#在线数据和离线数据对比相等就执行输入的章节不+1 time.sleep(2) all_no_list[b].click()#执行输入已看的章节,点击 WebDriverWait(driver, 10) # 等待响应 driver.switch_to.frame('iframe') # 进入第一层嵌套 time.sleep(2) over = driver.find_element(By.XPATH, '//*[@id="ext-gen1050"]')#获取视频页面的任务是否完成 if over.get_attribute('aria-label')=='任务点未完成':#未完成进入函数进行完成 Nested() Durationdetection() else: videomore_num = driver.find_elements(By.XPATH, '//*[@id="ext-gen1049"]/iframe') if len(videomore_num)>1: videomore=len(videomore_num) test() end_num = end_num - 1 unlist_num = unlist_num - 1 a = a + 1 driver.back() time.sleep(1) driver.back() else:#当在线数据与离线数据不一样时b+1 b = b + 1 with open("text.text", 'w', encoding="UTF-8") as f: f.write('已完成章节:'+str(b)) unlist_num = unlist_num + 1 time.sleep(2) all_no_list[b].click() WebDriverWait(driver, 10) # 等待响应 driver.switch_to.frame('iframe') # 进入第一层嵌套 time.sleep(2) over = driver.find_element(By.XPATH, '//*[@id="ext-gen1050"]') if over.get_attribute('aria-label') == '任务点未完成': Nested() Durationdetection() else: videomore_num = driver.find_elements(By.XPATH, '//*[@id="ext-gen1049"]/iframe') if len(videomore_num) > 1: videomore = len(videomore_num) test() end_num = end_num - 1 unlist_num = unlist_num - 1 a = a + 1 driver.back() time.sleep(1) driver.back() 章节学习

进入章节后点击学习(点击学习前需要进入iframe)并且每隔10秒的时间检测是否学习完了,是否有视频中间的测试题。

def Nested(): # 进入iframe嵌套点击开始 WebDriverWait(driver, 10) driver.switch_to.default_content() # 退出到主框架 driver.switch_to.frame('iframe') # 进入第一层嵌套 WebDriverWait(driver,10) a = driver.find_elements(By.XPATH, '//*[@id="ext-gen1049"]/iframe') if len(a)>0: global videomore#创建一个全局变量为ciodemore方便后面进行判断 videomore=len(a) driver.switch_to.frame(a[0]) # 进入第二层嵌套 WebDriverWait(driver, 10) driver.find_element(By.CLASS_NAME, "vjs-big-play-button").click() # 点击开始 def Durationdetection(): WebDriverWait(driver, 10) global unlist_num global a global c global end_num global videomore print('正在观看中......') pause_btn = driver.find_element(By.XPATH, '//button[contains(@class,"vjs-play-control")and ' 'contains(@class,"vjs-control")and contains(@class,"vjs-button")]')#获取相关信息 test_stop = driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/div[2]/div[1]/div[5]/button') while True: # 播放等待 time.sleep(10) # 每10秒,检查视频是否播放完毕 if (pause_btn.get_attribute('title') == "重播"): # 点击后播放,即播放完毕状态 if videomore>1: test() else: print('本小节以完成进入下一节') end_num = end_num - 1 unlist_num = unlist_num - 1 a = a + 1 break if (test_stop.get_attribute('title') == "播放"): time.sleep(2) try: test_stop.click() except: test_subject1() time.sleep(1) driver.back() time.sleep(1) driver.back() time.sleep(1) driver.back() 自动做视频章节的习题

视频中间会弹出习题,进行自动的答题(只写了判断题和选择题,没有写填空题)

def test_subject1(): time.sleep(1) test1 = driver.find_elements(By.CLASS_NAME, 'tkRadio') # 获取选项 test1[0].click() driver.find_element(By.XPATH, '//*[@id="videoquiz-submit"]').click() try: WebDriverWait(driver, 10) tf = driver.find_element(By.CLASS_NAME, 'tkTopic_oper').text # 获取正确和错误 print(tf, '换一个选项') time.sleep(1) test1[1].click() driver.find_element(By.XPATH, '//*[@id="videoquiz-submit"]').click() print('回答正确') except: print('回答正确') 检测每一章节是否有多个视频

检测每个章节是否有多个章节,如果有进入iframe全部学习完。

def test(): c=0 global videomore while True: if videomore==c+1: break c=c+1 driver.switch_to.default_content() driver.switch_to.frame('iframe') a=driver.find_elements(By.XPATH,'//*[@id="ext-gen1049"]/iframe') time.sleep(1) driver.switch_to.frame(a[c]) time.sleep(2) b=driver.find_element(By.XPATH,'//*[@id="video"]/button/span[2]') driver.execute_script("arguments[0].click();",b) pause_btn = driver.find_element(By.XPATH, '//button[contains(@class,"vjs-play-control")and ' 'contains(@class,"vjs-control")and contains(@class,"vjs-button")]') test_stop = driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/div[2]/div[1]/div[5]/button') while True: # 播放等待 time.sleep(10) # 每15秒,检查视频是否播放完毕 if (pause_btn.get_attribute('title') == "重播"): # 点击后播放,即播放完毕状态 break if (test_stop.get_attribute('title') == "播放"): time.sleep(2) try: test_stop.click() except: test_subject1() 总代码 import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.chrome.options import Options music = input('浏览器静音是否启动Y/N,默认为不启动') head = input('浏览器是否启用无窗口模式Y/N,默认为不启动') # 启动Chrome浏览器 chrome_options = Options() if music == 'Y': chrome_options.add_argument('--mute-audio') # 浏览器静音 if head == 'Y': chrome_options.add_argument('--headless') # 浏览器隐藏 driver = webdriver.Chrome(options=chrome_options) #让用户输入已看视频数默认为0 while True: video_num=input('输入已看的视频数/不输入表示延续上一次内容:') if video_num=='': break try: video_num=int(video_num) with open("text.text", 'w', encoding="UTF-8") as f: f.write('已完成章节:'+str(video_num)) break except: print('请输入整数') with open("text.text",encoding="UTF-8") as f: b=int(str(f.read()).split("已完成章节:")[1]) a=b a1=b c = 0 print('正在加载中请稍等......') def test(): c=0 global videomore while True: if videomore==c+1: break c=c+1 driver.switch_to.default_content() driver.switch_to.frame('iframe') a=driver.find_elements(By.XPATH,'//*[@id="ext-gen1049"]/iframe') time.sleep(1) driver.switch_to.frame(a[c]) time.sleep(2) b=driver.find_element(By.XPATH,'//*[@id="video"]/button/span[2]') driver.execute_script("arguments[0].click();",b) pause_btn = driver.find_element(By.XPATH, '//button[contains(@class,"vjs-play-control")and ' 'contains(@class,"vjs-control")and contains(@class,"vjs-button")]') test_stop = driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/div[2]/div[1]/div[5]/button') while True: # 播放等待 time.sleep(10) # 每15秒,检查视频是否播放完毕 if (pause_btn.get_attribute('title') == "重播"): # 点击后播放,即播放完毕状态 break if (test_stop.get_attribute('title') == "播放"): time.sleep(2) try: test_stop.click() except: test_subject1() def test_subject1(): time.sleep(1) test1 = driver.find_elements(By.CLASS_NAME, 'tkRadio') # 获取选项 test1[0].click() driver.find_element(By.XPATH, '//*[@id="videoquiz-submit"]').click() try: WebDriverWait(driver, 10) tf = driver.find_element(By.CLASS_NAME, 'tkTopic_oper').text # 获取正确和错误 print(tf, '换一个选项') time.sleep(1) test1[1].click() driver.find_element(By.XPATH, '//*[@id="videoquiz-submit"]').click() print('回答正确') except: print('回答正确') def Nested(): # 进入iframe嵌套点击开始 WebDriverWait(driver, 10) driver.switch_to.default_content() # 退出到主框架 driver.switch_to.frame('iframe') # 进入第一层嵌套 WebDriverWait(driver,10) a = driver.find_elements(By.XPATH, '//*[@id="ext-gen1049"]/iframe') if len(a)>0: global videomore#创建一个全局变量为ciodemore方便后面进行判断 videomore=len(a) driver.switch_to.frame(a[0]) # 进入第二层嵌套 WebDriverWait(driver, 10) driver.find_element(By.CLASS_NAME, "vjs-big-play-button").click() # 点击开始 def Durationdetection(): WebDriverWait(driver, 10) global unlist_num global a global c global end_num global videomore print('正在观看中......') pause_btn = driver.find_element(By.XPATH, '//button[contains(@class,"vjs-play-control")and ' 'contains(@class,"vjs-control")and contains(@class,"vjs-button")]')#获取相关信息 test_stop = driver.find_element(By.XPATH, '/html/body/div[2]/div[3]/div[2]/div[1]/div[5]/button') while True: # 播放等待 time.sleep(10) # 每10秒,检查视频是否播放完毕 if (pause_btn.get_attribute('title') == "重播"): # 点击后播放,即播放完毕状态 if videomore>1: test() else: print('本小节以完成进入下一节') end_num = end_num - 1 unlist_num = unlist_num - 1 a = a + 1 break if (test_stop.get_attribute('title') == "播放"): time.sleep(2) try: test_stop.click() except: test_subject1() time.sleep(1) driver.back() time.sleep(1) driver.back() time.sleep(1) driver.back() # -------------------------------------------------------------------------------------------------------------------------------- # 打开网站并进行登录 driver.get('https://passport2.chaoxing.com/login?fid=&refer=') phone = driver.find_element(By.CLASS_NAME, 'ipt-tel') pwd = driver.find_element(By.CLASS_NAME, 'ipt-pwd') login = driver.find_element(By.CLASS_NAME, 'btn-big-blue') while True: loginname = input("请输入用户名:") loginpassword = input("请输入密码:") phone.send_keys(loginname) pwd.send_keys(loginpassword) login.click()#点击登录 time.sleep(2) try: loginerr=driver.find_element(By.ID,'err-txt').text if loginerr=='手机号或密码错误': print(loginerr+'请重新输入') phone.clear() pwd.clear() except: print('登录成功') break time.sleep(2) driver.switch_to.frame("frame_content") time.sleep(2) tab=driver.find_elements(By.CLASS_NAME,'color1') chapters=0 for i in tab: chapters=chapters+1 print('第'+str(chapters)+'章《'+i.text+'》') choose=int(input('请输入学习第几章:')) choose=choose-1 chooselink=tab[choose].get_attribute('href')#获取对应章节的链接 driver.get(chooselink) driver.refresh() driver.switch_to.default_content() while True: WebDriverWait(driver, 10) # 等待响应 if a > a1: driver.refresh() time.sleep(2) driver.switch_to.default_content() WebDriverWait(driver,10) driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/div[1]/div/ul[1]/li[2]/a').click() frame_content = driver.find_element(By.XPATH, '//*[@id="frame_content-zj"]')#进入iframe driver.switch_to.frame(frame_content) all_no_list = driver.find_elements(By.XPATH, '//span[@class="catalog_points_yi"]')#查找所有未完成的知识点页面 inlist_num = len(all_no_list) # 在线数据(每次循环都会更新) if a == a1: unlist_num = len(all_no_list) # 离线数据(只是第一次循环更新) end_num = len(all_no_list)-b # 现有章节 allin_num = len(all_no_list) # 总章节 allin1 = allin_num - a print('总章节:', allin_num, '节') print('现有章节章节:', end_num, '节') print('已完成章节:', a, '节') print('剩余章节:', allin1, '节') print('----------------------------------------------------------------------') if end_num == 0: # 比对现有章节,没有知识点页面,即全部刷完 with open("text.text", 'w', encoding="UTF-8") as f: f.write('已完成章节:0') print('知识点已全部完成') break if unlist_num == inlist_num:#在线数据和离线数据对比相等就执行输入的章节不+1 time.sleep(2) all_no_list[b].click()#执行输入已看的章节,点击 WebDriverWait(driver, 10) # 等待响应 driver.switch_to.frame('iframe') # 进入第一层嵌套 time.sleep(2) over = driver.find_element(By.XPATH, '//*[@id="ext-gen1050"]')#获取视频页面的任务是否完成 if over.get_attribute('aria-label')=='任务点未完成':#未完成进入函数进行完成 Nested() Durationdetection() else: videomore_num = driver.find_elements(By.XPATH, '//*[@id="ext-gen1049"]/iframe') if len(videomore_num)>1: videomore=len(videomore_num) test() end_num = end_num - 1 unlist_num = unlist_num - 1 a = a + 1 driver.back() time.sleep(1) driver.back() else:#当在线数据与离线数据不一样时b+1 b = b + 1 with open("text.text", 'w', encoding="UTF-8") as f: f.write('已完成章节:'+str(b)) unlist_num = unlist_num + 1 time.sleep(2) all_no_list[b].click() WebDriverWait(driver, 10) # 等待响应 driver.switch_to.frame('iframe') # 进入第一层嵌套 time.sleep(2) over = driver.find_element(By.XPATH, '//*[@id="ext-gen1050"]') if over.get_attribute('aria-label') == '任务点未完成': Nested() Durationdetection() else: videomore_num = driver.find_elements(By.XPATH, '//*[@id="ext-gen1049"]/iframe') if len(videomore_num) > 1: videomore = len(videomore_num) test() end_num = end_num - 1 unlist_num = unlist_num - 1 a = a + 1 driver.back() time.sleep(1) driver.back()

这中间还有一些小的功能我没有介绍,我也想不起来了(都写了好久了但是现在应该还能用)

发出来只是和大家一起学习探讨。如有地方不合规请联系我删除,代码禁止用于非法用途。

未经本人允许不可转载。

 

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有