Selenium的webdriver能控制浏览器实现浏览器的一些操作。在看视频时,手可能在做其他事情,忙不过来换集,这个就可以帮助你自动换。


前言

    Selenium的webdriver能控制浏览器实现浏览器的一些操作。因此可以用它拿来实现我们的视频自动化。实现懒人模式哟,这样就可以不用腾出手来换集了。

关于如何配置和下载webdriver,见我的另外文章:Selenium webdriver安装配置_LLGululu的博客-CSDN博客


一、准备工具

1.正版谷歌浏览器(本文章用的是版本 105.0.5195.127(正式版本) (64 位))

2.谷歌浏览器对应版本的webdriver.exe

3.python3+(本文章用的是3.7)

二、分析

1.理解webdriver的使用

示例代码如下:

from selenium import webdriver
from selenium.webdriver import ActionChains


if __name__ == '__main__':
    # 启动浏览器
    driver = webdriver.Chrome()
    # 模拟动作
    action = ActionChains(driver)
    # 使浏览器打开时最大化,防止页面元素变化
    driver.maximize_window()
    url = 'https://www.baidu.com/'
    # 打开地址
    driver.get(url)
    # 防止网速过慢,页面未加载完全
    time.sleep(3)
    driver.quit()

 通过上方代码就可以实现,打开浏览器,百度。

通过find_element()找到需要点击的按钮或者元素,再通过action进行动作实现。

action除了上面展示的模拟鼠标移动、点击,还有右击、双击等等。


 下面介绍使用步骤和人工需要自己点击的地方。

1.配置自己的信息

        config.py是自己的信息配置文件,填写自己的账号密码还有视频的播放速度。ti 是你在登录选择视频时,留给你人工操作的时间,网络不好可以设置长一点。

代码如下:

# 账号
userid = ''
# 密码
password = ''
# 播放速率 1.5|1.25|1
video_speed = ''
# 手动操作时间设置 网络不好就设置久点
ti = 30

2.使用步骤之启动程序

        启动main.py后,自动启动浏览器,自动跳转到网站,然后会自动点击登录按钮跳转到登录界面,自动输入账号密码,并点击登录。


 3.使用步骤之手动操作过程

   请在config.py文件中 ti 设置时间范围内完成接下来的操作:

        登录后弹出滑动验证,这需要我们人工进行操作。如下图:

b33a58e385034c5e9be2221da3df6bad.png

         滑动验证通过后,就会c进入页面。这里请人工手动选择自己的视频。

         进入播放页面后,由于第一次打开会弹出提醒你的弹窗,请手动关闭它们 。

         最后选择你的开始视频(程序只会从你开始的视频到最后一个,不会识别你哪些未看!!!)请让页面保持视频播放初始页面(视频并未播放、速率并未调整、未静音。这些程序会自动设置的)。这样就可以了!!!


 三、代码实现

 1.配置文件config.py

# 账号
userid = ''
# 密码
password = ''
# 播放速率 1.5|1.25|1
video_speed = '1.5'
# 手动操作时间设置 网络不好就设置久点
ti = 30

 2.运行文件main.py

import re
import time
import config
import schedule
from selenium import webdriver
from selenium.webdriver import ActionChains

import getXpath
import list_index
import monitor
import video_control


# 监测视频时间进度
def getTime(driver0, list_title0, number_list0):
    text0 = driver0.page_source
    try:
        current_time = re.findall('class="currentTime">(.*?)</span>', text0)[0]
        total_time = re.findall('class="duration">(.*?)</span>', text0)[0]
        # print(current_time)
        # print(total_time)
        checkIsTime(current_time, total_time, text0, number_list0, driver0, list_title0)
    except:
        print('未找到时间')


# 视频当前时间和总时长
def checkIsTime(current_time, total_time, text0, number_list0, driver0, list_title0):
    if current_time == total_time:
        now_video = re.findall('id="lessonOrder" title="(.*?)"', text0)[0]
        now_video = now_video.split('、')
        del now_video[0]
        now_video = '、'.join(now_video)
        index_title0 = list_title0.index(now_video)
        turn_next(number_list0, driver0, list_title0, index_title0)


# 跳转下一条视频
def turn_next(number_list0, driver0, list_title0, index_title0):
    # 获得下一视频的xpath
    # 跳转到下一视频

    action0 = ActionChains(driver0)
    next_number = number_list0[index_title0 + 1]
    xpath = getXpath.getXpth(next_number)
    e = driver0.find_element('xpath', xpath)
    action0.click(e).perform()
    time.sleep(2)

    video_control.video_control(driver0)
    print('当前视频已经结束!跳转下一个视频:' + list_title0[index_title0 + 1])
    global i
    i = i + 1


if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.maximize_window()
    url = ''
    driver.get(url)
    # time.sleep(3)
    action = ActionChains(driver)
    e = driver.find_element('xpath', '//*[@id="notLogin"]/span/a[1]')
    action.move_to_element(e).perform()
    action.click(e).perform()
    e = driver.find_element('xpath', '//*[@id="lUsername"]')
    e.send_keys(config.userid)
    e = driver.find_element('xpath', '//*[@id="lPassword"]')
    e.send_keys(config.password)
    e = driver.find_element('xpath', '//*[@id="f_sign_up"]/div[1]/span')
    action.move_to_element(e).perform()
    action.click(e).perform()
    # 设置等待时间  网络不好的就把时间设置长点
    time.sleep(config.ti)
    text = driver.page_source
    list_title = re.findall('class="catalogue_title">(.*?)</span>', text)
    start_video = re.findall('id="lessonOrder" title="(.*?)"', text)[0]
    start_video = start_video.split('、')
    del start_video[0]
    start_video = '、'.join(start_video)
    print('开始视频:' + start_video)
    i = list_title.index(start_video)
    # 播放视频等操作
    video_control.video_control(driver)
    # 获得开始视频编号
    index_title = list_title.index(start_video)
    # 获得编号列表 如4.1.2 、4.2
    number_list = list_index.getlist(text)
    # print(number_list)

    # 弹窗测试
    schedule.every(10).seconds.do(monitor.pop_up, driver)
    # 时间检测
    schedule.every(10).seconds.do(getTime, driver, list_title, number_list)
    while i < len(list_title):
        schedule.run_pending()

3.检测是否有弹窗monitor.py

# 监视操作

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


# 弹窗测试的选项按钮  class='topic-option-item'
# 弹窗测试的关闭按钮  class="btn"
def pop_up(driver):
    action = ActionChains(driver)
    try:
        # 选第一个
        e = driver.find_element(By.CLASS_NAME, 'topic-option-item')
        action.click(e).perform()
        print('弹窗测试....')
        # 关闭弹窗
        e = driver.find_element('xpath', '//*[@id="playTopic-dialog"]/div/div[3]/span/div')
        action.move_to_element(e)
        action.click(e).perform()
        # 播放视频
        e = driver.find_element('xpath', '//*[@id="vjs_container"]/div[8]')
        action.move_to_element(e).perform()
        action.click(e).perform()
    except:
        a=1

4.掉整视频的播放video_control.py

from selenium.webdriver import ActionChains
import config


# 播放视频等操作
def video_control(driver):
    action = ActionChains(driver)
    # 播放速度设置
    video_speed = config.video_speed
    e = driver.find_element('xpath', '//*[@id="vjs_container"]/div[8]')
    action.move_to_element(e).perform()
    e = driver.find_element('xpath', '//*[@id="vjs_container"]/div[10]/div[8]/span')
    action.move_to_element(e).perform()
    if video_speed == '1.5':
        e = driver.find_element('xpath', '//*[@id="vjs_container"]/div[10]/div[8]/div/div[1]')
        action.click(e).perform()
    elif video_speed == '1.25':
        e = driver.find_element('xpath', '//*[@id="vjs_container"]/div[10]/div[8]/div/div[2]')
        action.click(e).perform()
    # 视频静音
    e = driver.find_element('xpath', '//*[@id="vjs_container"]/div[8]')
    action.move_to_element(e).perform()
    e = driver.find_element('xpath', '//*[@id="vjs_container"]/div[10]/div[6]')
    action.move_to_element(e).perform()
    action.click(e).perform()
    # 播放视频
    e = driver.find_element('xpath', '//*[@id="vjs_container"]/div[8]')
    action.move_to_element(e).perform()
    action.click(e).perform()

5.获得播放集数列表list_index.py

import re


# 获得能点击交互列表的标号  用来获得xpath地址  
def getlist(text):
    number_list0 = re.findall('class="pl5  hour">(.*?)</b>', text)
    # print(number_list0)
    # 存放拥有三级列表的二级列表
    number_list1 = []
    for i in number_list0:
        if i.count('.') == 2:
            list0 = i.split('.')
            del list0[len(list0) - 1]
            str0 = '.'.join(list0)
            if str0 not in number_list1:
                number_list1.append(str0)
    for j in number_list1:
        index0 = number_list0.index(j)
        del number_list0[index0]
    return number_list0

6.xpath编写getXpath.py

# 获得下一视频的xpath地址
def getXpth(number):
    list0 = number.split('.')
    if number.count('.') == 2:

        xpath = '//*[@id="app"]/div/div[2]/div[2]/div[2]/div[1]/div/ul[' + list0[0] + ']/div[' + list0[
            1] + ']/ul/li[' \
                + list0[2] + ']/div '
        return xpath
    else:
        xpath = '//*[@id="app"]/div/div[2]/div[2]/div[2]/div[1]/div/ul[' + list0[0] + ']/div[' + list0[1] + ']/li/div'
        return xpath




看到这里觉得有用就点个赞吧!!!dc6fc058b35e4df5b3098ae84e34e491.png

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐