关于python:Python-Selenium实现自动观看学习通视频

24次阅读

共计 2279 个字符,预计需要花费 6 分钟才能阅读完成。

import time
from selenium.webdriver import Chrome
web = Chrome()
web.get(‘https://mooc2-ans.chaoxing.co…’)

1. 登录

phone = web.find_element_by_class_name(‘ipt-tel’)
pwd = web.find_element_by_class_name(‘ipt-pwd’)
login = web.find_element_by_class_name(‘btn-big-blue’)
phone.send_keys(‘ 手机号码 ’)
pwd.send_keys(‘ 明码 ’)
login.click()
time.sleep(2)

实现一个页面的所有未实现的知识点

def view_one_page_points():

while (1):
    iframe = web.find_element_by_id('iframe')  # 每次刷新后,都要进入外部 iframe
    web.switch_to.frame(iframe)
    # 筛选,去除已实现的知识点
    k_points = web.find_elements_by_css_selector('div.ans-attach-ct:not(.ans-job-finished)')
    num = len(k_points)
    flag = False
    for i in range(0, num):
        if i == (num - 1):  # 是最初一个,示意该页刷完
            flag = True
        k_point = k_points[i]
        # 查找工作图标个数,1 为知识点,0 为不是知识点
        icon_num = len(k_point.find_elements_by_xpath('./div[contains(@class,"ans-job-icon")]'))
        if icon_num == 0:  # 再次筛选,去除不是知识点的 div
            continue
        # 进行视频的播放
        video_iframe = k_point.find_element_by_xpath('./iframe')  # 视频 iframe
        print(video_iframe)
        time.sleep(2)
        web.switch_to.frame(video_iframe)  # 进入视频 iframe
        time.sleep(2)
        web.find_element_by_class_name('vjs-big-play-button').click()  # 点击播放按钮
        time.sleep(1)
        web.find_element_by_xpath('//*[@id="video"]/div[5]/div[6]/button').click()  # 静音
        # 播放和暂停按钮
        pause_btn = web.find_element_by_xpath('//button[contains(@class,"vjs-play-control")and'
                                              'contains(@class,"vjs-control")and contains(@class,"vjs-button")]')
        while (1):  # 播放期待
            time.sleep(1)  # 每 1 秒,查看视频是否播放结束
            if (pause_btn.get_attribute('title') == "重播"):  # 点击后播放,即播放结束状态
                break
        print('视频播放结束')
        # 视频播放结束,退出播放 iframe,而后退出循环,再次查找该页面的所有知识点(k_points)
        web.switch_to.default_content()
        print('退出知识点 iframe')
        time.sleep(2)
        web.refresh()  # 刷新页面,之后需从新进入 iframe
        time.sleep(2)
        print('刷新页面')
        break
    if flag:  # 该页面知识点播放结束
        break
pass

while (1):

# 进入 iframe
frame_content = web.find_element_by_xpath('//*[@id="frame_content-zj"]')
web.switch_to.frame(frame_content)
time.sleep(2)
# 查找所有未实现的知识点页面
all_no_list = web.find_elements_by_xpath('//span[@class="catalog_points_yi"]')
list_num = len(all_no_list)  #知识点页面个数
if list_num == 0:   # 没有知识点页面,即全副刷完
    break
# 跳转到第一个知识点页面
all_no_list[0].click()
web.switch_to.window(web.window_handles[-1])  # 跳转到该知识点界面
time.sleep(5)
#####
view_one_page_points()  # 播放该知识点页面的所有未实现的知识点视频
#####
print('实现一个知识点页面...')
web.close()  # [Skrill 下载](https://www.gendan5.com/wallet/Skrill.html) 敞开以后窗口
# 该页面知识点结束,敞开以后窗口,抉择下一个知识点窗口
web.switch_to.window(web.window_handles[0])  # 变更视角到该课程主界面
time.sleep(1)
# 刷新页面
web.refresh()
time.sleep(2)
print('刷新主页面')
pass
正文完
 0