标签 自动学习脚本 下的文章

背景

学习公社平台学习中,需要人工干预才可以连续学习。

安装油猴

自己百度

导入脚本

// ==UserScript==
// home.php?mod=space&uid=170990         学习公社平台
// home.php?mod=space&uid=467642    http://tampermonkey.net/
// home.php?mod=space&uid=1248337      0.3
// @description  try to take over the world!
// home.php?mod=space&uid=686208       You
// home.php?mod=space&uid=195849        https://study.enaea.edu.cn/viewerforccvideo.do?courseId=*&circleId=*
// home.php?mod=space&uid=593100         https://www.google.com/s2/favicons?domain=enaea.edu.cn
// home.php?mod=space&uid=609072        none
// ==/UserScript==

(function() {
    'use strict';

    let isLast = false;
     function gotoNext(){
         let todos = opener.$('table.dataTable tr:contains(%)').filter((i,v)=>{ return $(v).find('td:nth-child(4)').text().indexOf('#') == -1 &&  $(v).find('td:nth-child(5)').text().indexOf('100%') == -1})
         .filter((i,v)=>{ let vurl = $(v).find('a.saveStuCourse').attr('data-vurl'); return location.href.indexOf(vurl) == -1});
            if(todos.length > 0){
                console.log('switch to next.... reload  ');
                let nextCourse =  $(todos[0]);
                nextCourse.find('td').eq(3).text('#')
                let nextVurl = nextCourse.find('a.saveStuCourse').attr('data-vurl');
                location.href = nextVurl;
            }
    }

    function autoSetEvent(){
        setInterval(function(){
            var aud = $("div video").get(0);
            if( $(aud).prop('__ok__') != true){
                aud.addEventListener("ended", video_ended);
                console.log("add ended event listener.")
                $(aud).prop('__ok__',true);

                isLast = $('ul.cvtb-MCK-course-list li.last').text() == $('ul.cvtb-MCK-course-list li.current').text();

                let notStudyList = $('div.cvtb-MCK-CsCt-studyProgress').filter((i,v)=>$(v).text().indexOf('100%') == -1);
                if(notStudyList.length == 0){
                    gotoNext();
                }
                else if(notStudyList.length > 0 && $('ul.cvtb-MCK-course-list li.current').find('div.cvtb-MCK-CsCt-studyProgress').text().indexOf('100%') === 0){
                        notStudyList.eq(0).parent().click();
                }
            }

            $('.dialog-button-container button:contains(继续学习)').click()
        },3000);
    }

    function video_ended() {
        console.log("The audio has ended");

        if(isLast){
            console.log('switch to next.... ');       

           gotoNext();
        }
    }

    setTimeout(function(){
        // debugger;
        $(".continue .user_choise").click()
        autoSetEvent();
    },3000);

    // Your code here...
})();

正文

我的chrome没有适合的驱动版本,又不想卸载重装,于是就用了edge,同时将edge停掉了自动更新,防止更新后驱动不能用
使用前,需要先自选课程,多说一句,本来用selenium弄了自选的代码,结果偶尔会报错,就懒得弄了。选好课程后,启动程序将自动学习,直到你的自选课程学完或者学够了50分,程序将停止
为了不影响电脑的前台操作,将窗口后台运行了,并关掉了音量,自己运行时,可以看一下注释,想听声音或者前台运行,可以将相应的代码注释掉就可以了

以下为代码

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
 
def start():
    browser.get("http://www.hbgbzx.gov.cn/login.html")  # 打开干部在线
    time.sleep(3)
    login(account,password)
 
def login(account,password):
    browser.find_element(By.ID, 'name').send_keys(account)  # 输入账号
    time.sleep(2)
    browser.find_element(By.ID, "password").send_keys(password) # 输入密码
    time.sleep(2)
    browser.find_element(By.XPATH, '//*[@id="form1"]/div/div/ul/li[3]/input').click()   # 点击确定
    time.sleep(3)
    print('------登录成功!!-----')
    browser.switch_to.alert.accept()    # 跳过登陆后的弹窗
    time.sleep(2)
    browser.find_element(By.XPATH, '//*[@id="menu3"]/p').click()  # 打开我的课程
    time.sleep(2)
    browser.find_element(By.XPATH, '//*[@id="sonmenu3"]/div[1]/a').click()  # 打开自选课程
    time.sleep(2)
    study()
 
def study():
    browser.find_element(By.XPATH, '//*[@id="right"]/div[4]/table/tbody/tr[3]/td/a[1]').click()  # 点击我要学习按钮
    name=browser.find_element(By.XPATH,'//*[@id="right"]/div[4]/table/tbody/tr[1]/td/span').text    # 课程的名字
    # 课程的时长
    minute=browser.find_element(By.XPATH,'//*[@id="right"]/div[4]/table/tbody/tr[2]/td/div[4]/span[2]').text
    time.sleep(2)
    print(f'开始学习!')
    print(f'{name}')
    print(f'时长{minute}')
    browser.switch_to.window(browser.window_handles[1]) # 切换到视频学习网页的页面
    # 跳过网页内的双层iframe嵌套
    browser.switch_to.frame('course_frm')
    browser.switch_to.frame('course_frame')
    browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]').click()  # 点击开始学习或者继续学习按钮
    time.sleep(3)
    browser.switch_to.window(browser.window_handles[0]) # 切换回自选课程页面
    browser.refresh()   # 刷新网页
    choose(count)
 
def choose(count):
    # 获取左下角的总分数
    score = browser.find_element(By.XPATH, '//*[@id="xyxx"]/div[4]/span').text
    print(f'现在总分为:{score}')
    # 获取当前学习课程的分数
    # percent = browser.find_element(By.XPATH, '//*[@id="right"]/div[4]/table/tbody/tr[2]/td/div[2]/span/span').text
    # 判断总分数是否大于等于50分,若大于等于50分,则退出程序
    if score<'50.00':
        while True:
            # 将总分数赋值给m,用以判断程序的运行
            time.sleep(60)  # 每60秒刷新一次网页,并记录刷新网页的次数
            browser.refresh()
            count += 1
            print(f'第{count}次刷新页面,60秒后再次刷新页面')
            # 页面刷新后将总分数再次赋值给x,用以判断是否调用study()函数
            x=browser.find_element(By.XPATH, '//*[@id="xyxx"]/div[4]/span').text
            # 学习进度情况
            percent=browser.find_element(By.XPATH,'//*[@id="right"]/div[4]/table/tbody/tr[3]/td/span').text
            print(f'当前学习进度为:{percent}')
            if x<'50.00':
                study()
            # elif m<x:
            #     study()
            else:
                print('已经大于50分,程序退出')
                break
    else:
        browser.quit()
        print('已经学习了50分,本次学习结束')
 
if __name__ == '__main__':
    account=input('请输入你的账号:')
    password=input('请输入你的密码:')
    count = 0
    edge_driver = Service(r"E:\\edgedriver\\msedgedriver.exe")  # 调用edge浏览器的驱动程序
    option=webdriver.EdgeOptions()
    option.use_chromium=True
    option.add_argument('headless')     # 将浏览器后台运行,不影响前台的操作
    option.add_argument('disable-gpu')
    option.add_argument('--mute-audio') # 关闭浏览器内的声音
    browser = webdriver.Edge(service=edge_driver,options=option)
    start()