自动刷课利器学习通,轻松摆脱大学网课手动点击烦恼

用python的实现的一个脚本
1.需要chrome浏览器
2.selenium库
3.驱动程序chromedriver(对应Chrome版本)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time

# 设置 Chrome 浏览器选项
chrome_options = Options()
chrome_options.add_argument("--start-maximized")  # 最大化窗口
# 如果需要无头模式,可以取消下面这行的注释
# chrome_options.add_argument("--headless")

# 创建浏览器对象
driver = webdriver.Chrome(options=chrome_options)

# 打开目标网页
url = '学习通网址'
driver.get(url)

# 提示用户手动登录
print("请手动登录网页,登录完成后按回车键继续...")
input()  # 等待用户按下回车键

# 等待页面加载完成
wait = WebDriverWait(driver, 30)  # 增加等待时间为 30 秒


def play_video(video_element):
    """播放视频并设置为 2 倍速"""
    try:
        # 使用 JavaScript 修改视频播放速度为 2 倍速
        playback_speed = 2.0
        driver.execute_script(f"arguments[0].playbackRate = {playback_speed};", video_element)
        print(f"已将播放速度设置为 {playback_speed} 倍速。")

        # 播放视频
        driver.execute_script("arguments[0].play();", video_element)
        print("视频已开始播放。")

        # 等待视频播放完成
        while True:
            is_ended = driver.execute_script("return arguments[0].ended;", video_element)
            if is_ended:
                print("视频播放完成!")
                break
            time.sleep(1)  # 每秒检查一次视频是否结束

    except Exception as e:
        print(f"播放视频时发生错误: {e}")


def find_and_play_all_videos():
    """找到播放列表中的所有视频并逐一播放"""
    try:
        # 查找所有章节列表项
        chapters = driver.find_elements(By.CLASS_NAME, 'posCatalog_name')
        for index, chapter in enumerate(chapters):
            print(f"正在处理第 {index + 1} 个章节: {chapter.text}")

            # 点击章节
            chapter.click()
            time.sleep(5)  # 等待页面刷新

            # 切换到第一个 iframe
            first_iframe = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'iframe')))
            driver.switch_to.frame(first_iframe)
            print("已切换到第一个 iframe。")

            # 切换到第二个 iframe
            second_iframe = wait.until(EC.presence_of_element_located((By.TAG_NAME, 'iframe')))
            driver.switch_to.frame(second_iframe)
            print("已切换到第二个 iframe。")

            # 查找视频元素
            try:
                video_element = wait.until(EC.presence_of_element_located((By.ID, 'video_html5_api')))
                print("找到视频元素!")

                # 查找播放按钮并点击
                play_button = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'vjs-big-play-button')))
                ActionChains(driver).move_to_element(play_button).click().perform()
                print("点击播放按钮成功!")

                # 播放视频
                play_video(video_element)

            except Exception as e:
                print(f"未找到视频元素或播放失败: {e}")

            finally:
                # 切换回默认内容
                driver.switch_to.default_content()

        print("所有章节均已处理完毕!")

    except Exception as e:
        print(f"查找和播放视频时发生错误: {e}")


try:
    # 开始处理播放列表中的所有视频
    find_and_play_all_videos()

except Exception as e:
    print(f"发生错误: {e}")

finally:
    # 关闭浏览器
    driver.quit()

作者:苏打饼丫

物联沃分享整理
物联沃-IOTWORD物联网 » 自动刷课利器学习通,轻松摆脱大学网课手动点击烦恼

发表回复