# 知到(智慧树)刷课 # python # DrissionPage # Actions

应用场景:(知到)智慧树刷课,最近期末了,但是好多课没看,就做了一个智慧树刷课程序,由于时间比较紧,可能会出现一些场景不能正常运行。但是正常使用没什么问题。

代码作用:可以索引找到没有观看的视频自动打开观看,中间出现选择题人机检测会自动选择正确选项并关闭播放视频,进度达到后会自动跳转下一个未达标的视频。

代码使用:

导入DrissionPage模块——复制下方代码——运行——输入需刷课程全称——回车即可

代码运行过程:

在共享课下方找到相关课程后进入——关闭弹出学前须知串口(如果没有关闭成功需手动关闭)——遍历课程——找到没有达标的课程——进入课程——播放视频并静音并X1.5——出现选择时选出正确选项并关闭并播放视频——视频达标后——向下选择未达标的视频并播放…………

应用限制:浏览器要在后台工作,由于作者学习通上面课程中间弹出的都是单选题,所有此代码没有添加多选的情况。

适合这种形式,因为是以@播放图标和后面的进度是否完成定位的。没有两个东西代码并不能运行

运行结果:

刷课结束时会出现:

from DrissionPage import ChromiumPage
from DrissionPage.common import Actions
from DrissionPage.errors import ElementNotFoundError

class_name = input('课程名称:')
rule_time = 3
seep_time = 2
tab_1 = ChromiumPage().latest_tab
ac = Actions(tab_1)

# 进入相应课堂
def Input_class(tab, class_name):
    in_class_s = tab.ele('@id=sharingClassed', timeout=rule_time)
    if in_class_s:
        in_class = in_class_s.ele(f'@text()={class_name}', timeout=rule_time)
        if in_class:
            try:
                in_class.click(timeout=rule_time)
                print(f'成功点击课堂:{class_name}')
                return 1
            except ElementNotFoundError as e:
                print(f'点击课堂时发生错误:{e}')
                return 0
        else:
            print(f'没有找到相应课堂:{class_name}')
            return 0
    else:
        print('没找到共享课堂?')
        return 0

# 取消弹出学前必读0.0
def Out_window_0():
    top_nf = tab_1.ele('x://*[@id="app"]/div/div[6]', timeout=rule_time)
    length = len(str(top_nf))
    if length == 64:
        top_out = tab_1.ele('@class=iconfont iconguanbi', timeout=rule_time)
        if top_out:
            try:
                top_out.click(timeout=rule_time)
                print('点击取消学前必读标签页成功!')
                return 1
            except ElementNotFoundError as e:
                print(f'点击取消学前必读标签页错误{e}')
                return 0
        else:
            print('没有找到取消标签页的X号')
            return 0
    else:
        print('无需取消标签页')
        return 0

def Watch_video():
    tab_1.ele('x://*[@id="vjs_container"]/div[8]').click()
    print('选择选后成功播放视频')

def Watch_video_1():
    tab_1.ele('@class=volumeBox').click()
    tab_1.ele('@class=playButton').click()
    tab_1.ele('@class=speedBox').click()
    tab_1.ele('@text()=X 1.5').click()
    print('点击观看、静音、1.5倍成功')

def Tell_pick():
    have = tab_1.ele('@aria-label=弹题测验')
    picks = tab_1.eles('@class=icon topic-option')
    if have:
        a = picks[0]
        if a:
            a.click()
            print('成功引出正确选项')
        else:
            print('没有选项A')
        tur_pick = tab_1.ele('@class=answer')
        if tur_pick:
            pick = tur_pick.text
            return pick[5:6]
    else:
        print('没有选择题1.0?')

def Pick_one(pick):
    have = tab_1.ele('@aria-label=弹题测验')
    close = have.ele('@class=el-dialog__headerbtn')
    picks = tab_1.eles('@class=icon topic-option')
    if have and close:
        if pick == 'A':
            picks[0].click()
            close.click()
        elif pick == 'B':
            picks[1].click()
            close.click()
        elif pick == 'C':
            picks[2].click()
            close.click()
        elif pick == 'D':
            picks[3].click()
            close.click()
    else:
        print('没有选择题2.0?或者没有找到关闭按钮')

def In_to_need_watch_video():
    our_video = tab_1.eles('@class=fl cataloguediv-c', timeout=rule_time)
    if our_video:
        to_time = 0
        for i in our_video:
            need_video = i.ele('@class=time_ico_half fl', timeout=rule_time)
            out_video = i.ele('@class=fl time_icofinish', timeout=rule_time)
            if not need_video and not out_video:
                continue
            if need_video and out_video:
                print(i.text + '已观看')
            else:
                print(i.text + '未观看')
                try:
                    need_video.click(timeout=rule_time)
                    tab_1.wait(1)           # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 重要,依据网速设置时间
                    Watch_video_1()
                    print(i.text + '点击观看视频成功')
                except ElementNotFoundError:
                    print(i.text + '点击观看视频错误?')
                while True:
                    have = tab_1.ele('@aria-label=弹题测验')
                    if have:
                        Pick_one(Tell_pick())
                        tab_1.wait(rule_time)
                        Watch_video()
                    need_video = i.ele('@class=time_ico_half fl', timeout=rule_time)
                    out_video = i.ele('@class=fl time_icofinish', timeout=rule_time)
                    tab_1.wait(rule_time)
                    if need_video and out_video:
                        to_time = 1
                        break
            if to_time == 1:
                print(i.text + '播放完毕')
                continue
    else:
        print('没有视频元素??')

print(Input_class(tab=tab_1, class_name=class_name))
tab_1.wait(seep_time)
print(Out_window_0())
tab_1.wait(seep_time)
print(In_to_need_watch_video())

作者:芋弦

物联沃分享整理
物联沃-IOTWORD物联网 » # 知到(智慧树)刷课 # python # DrissionPage # Actions

发表回复