这里用的是Chrome浏览器,使用selenium库是需要先安装Chrome的驱动
分析:
selenium库能实现网页自动操作,智慧树每次学习完一小节后都需要点击下一节视频。这里我检测了视频的总播放长度和当前播放的时间长度,每5秒钟检测一次它们是否相同,如果相同就点击下一节视频完成自动化。看视频时也会遇到不定时弹窗,就每隔5秒钟检测是否有弹窗出现,如果有弹窗就点击选项然后关闭。大概的思路就是这样。
如果有什么地方侵权了请联系我删除
我只是为了能和大家一起学习,没有其他意思
#导入库
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import os
import time
# 点击开始
def play():
start_status = driver.find_element(By.XPATH, '//div[@id="playButton"]').get_attribute('class')
start_button = driver.find_element(By.ID, 'playButton')
xuanting = driver.find_element(By.XPATH, '//*[@id="vjs_container"]/div[8]') # 点击视频
jianchetanchuang() # 检测弹窗
time.sleep(2)
if start_status.find('playButton') != -1:
print('当前静止')
xuanting.click()
print('点击播放成功')
else:
print('当前正在播放')
# 悬停找到控制台
def openControlsBar():
controlsBar = driver.find_element(By.XPATH, ' //div[@id="container"]')
ActionChains(driver).move_to_element(controlsBar).perform() ##
# 下一个视频
def nextVideo():
openControlsBar()
nextBtn = driver.find_element(By.XPATH, '//div[@id="nextBtn"]')
nextBtn.click()
print('切换下一个视频')
# 获取当前播放时间
def getCurrentTime():
openControlsBar()
time = driver.find_element(By.XPATH, '//div[@class="nPlayTime"]/span[1]').text
print('当前播放时间为:' + time)
return time
# 关闭弹窗继续播放
def pop_up():
panduan = driver.find_element(By.XPATH,
'//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/p/span[2]').text
if panduan in '判断题':
driver.find_element(By.XPATH,
'//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/ul/li[1]/div[2]').click() # 点击第一个选项
driver.find_element(By.XPATH, '//*[@id="playTopic-dialog"]/div/div[3]/span/div').click() # 点击关闭
else:
driver.find_element(By.XPATH,
'//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/ul/li[1]').click() # 点击第一个
driver.find_element(By.XPATH, '//*[@id="playTopic-dialog"]/div/div[3]/span/div').click() # 点击关闭
# chuowu = driver.find_element(By.XPATH,'//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/p/span[1]/span').text#获取是否正确
# driver.find_element(By.XPATH,'//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/ul/li[2]/div[2]')#点击错
# if chuowu == '正确':
# driver.find_element(By.XPATH,'//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/ul/li[1]/div[2]')#点击对
# driver.find_element(By.XPATH,'//*[@id="playTopic-dialog"]/div/div[3]/span/div').click()#点击关闭
# 关闭学前必读
def xueqian():
try:
# 检测是否有学前必读
# 点击×
close = driver.find_element(By.XPATH, '// *[ @ id = "app"] / div / div[6] / div[2] / div[1] / i')
close.click()
print("关闭学前必读成功!")
except:
print("未发现学前必读")
# 获取当前视频总时间
def getTotalTime():
openControlsBar()
time = driver.find_element(By.XPATH, '//div[@class="nPlayTime"]/span[2]').text
print('本集视频时间为:' + time)
return time
#控制下面的循环间隔
def sleeptime(hour, min, sec):
return hour * 3600 + min * 60 + sec
#检测弹窗
def jianchetanchuang(): # 检测弹窗
try:
pop_up() # 关闭弹窗
print('关闭弹窗完成')
except:
print('未检测到弹窗')
# 不自动关闭浏览器
option = webdriver.ChromeOptions()
option.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=option)
#主程序
driver.get('智慧树视频链接地址')
userName = "填写账号"
passWord = "填写密码"
driver.find_element(By.ID, 'lUsername').send_keys(userName)
driver.find_element(By.ID, 'lPassword').send_keys(passWord)
driver.find_element(By.XPATH, "//span[@class='wall-sub-btn']").click()
time.sleep(2)
yanzhi = driver.find_element(By.XPATH, '/html/body/div[29]/div[2]/div/div/div[1]/span[2]').text
second = sleeptime(0, 0, 5)
if yanzhi == '请完成安全验证':
print('完成验证按任意键继续')
os.system('pause')
# 检测是否看完了
while (True):
time.sleep(second) # 5秒执行一次
#关闭学前必读
xueqian()
# 检测弹窗
jianchetanchuang()
if getCurrentTime() == getTotalTime():
nextVideo()
time.sleep(5)
play()
版权归原作者 不爱写程序的小扬 所有, 如有侵权,请联系我们删除。