python实现学习通刷网课视频

使用前需要导入selenuim库和Chrome.driver,前者可以通过命令行下载,后者可以用过浏览器下载,此段代码仅实现刷一个小节中只有一个视频的情况,不支持多个视频,不支持刷ppt同时也不支持刷题,有需要的人可以自行编写相应功能。以下是代码

from selenium import webdriver
import time
import random
#打开谷歌浏览器,打开超星学习通登录入口
 
browser = webdriver.Chrome(executable_path="C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")#此处是chromedriver在电脑的绝对位置
url = 'http://i.mooc.chaoxing.com/space/index.shtml'#学习通首页网址
browser.get(url)
 
 
# 学校 学号 密码 验证码登陆
def login():
    inp_1 =input('请输入电话号码:')
    inp_2 =input('请输入密码:')
    inp_3 = input("请输入验证码:")
    #school = browser.find_element_by_id("FidName")
    username = browser.find_element_by_id("unameId")
    password = browser.find_element_by_id("passwordId")
    verycode = browser.find_element_by_id("numcode")
    #检查页面元素,修改相应ID
    #school.send_keys(inp_1)
    username.send_keys(inp_1)
    password.send_keys(inp_2)
    verycode.send_keys(inp_3)
    browser.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[7]/td[2]/label/input').click()   #检查页面元素,复制登录按钮的xpath
    print("----------正在登陆中----------")
 
 
# 进入主页 开始选择课程 xpath匹配
def html_1():
    time.sleep(random.random()*3)
    #选择我需要学的课的xpath
    #browser.find_element_by_xpath('//*[@id="zla_title"]"]').click()
    print("----------进入课程----------")
    time.sleep(2)
    browser.switch_to.frame('frame_content')
    browser.find_element_by_xpath('/html/body/div/div[2]/div[2]/ul/li[14]/div[1]/a[1]/img').click()  # 对应课程选项的xpath
    print("----------已进入所点击课程----------")
 
 
def html_2():
    # 浏览器标签页跳转设置
    browser.switch_to.window(browser.window_handles[1])
 
    # 进入课程中的指定小节
    time.sleep(3)
    browser.find_element_by_xpath('//*[@id="191256385"]/h3[1]/span[2]/a').click()  # 指定小节的xpath
    print('已进入指定小节')
    time.sleep(random.random()*3)
    
 
 
# button模拟点击播放
def button():
    time.sleep(random.random()*5)
    try:
        # 进入frame播放框架
        browser.switch_to.frame('iframe')
        frame=browser.find_element_by_xpath('//*[@id="ext-gen1038"]/div/div/p/div/iframe')
        browser.switch_to.frame(frame)
        time.sleep(random.random()*5)  # 此处等待需要就一些就不会报错终止程序
        browser.find_element_by_xpath('//*[@id="video"]/button').click()
        print('----------等待播放----------')
        # browser.find_element_by_xpath('//*[@id="video"]/div[5]/div[6]/button').click()
        # print('页面已静音')
        time.sleep(2)
        print("----------课程正在播放中----------")
 
    except Exception as e:
        print('Error at:',e.args[0],e.args[1])
        # 回到主框架
        browser.switch_to.default_content()
        print("----------此处不是视频,即将点击下一页----------")
        time.sleep(4)
        browser.find_element_by_xpath('//*[@id="mainid"]/div[1]/div[2]').click()
        time.sleep(4)
        # 判断完重新回到button函数再进行判断
        button()
 
 
# 判断视频是否播放完
def vedio_if():
    time.sleep(1)
    try:
        vedio_stat_time = browser.find_element_by_xpath('//*[@id="video"]/div[5]/div[2]/span[2]').get_attribute(
            "textContent")#此处是视频已播放时间的xpath
        vedio_end_time = browser.find_element_by_xpath('//*[@id="video"]/div[5]/div[4]/span[2]').get_attribute(
            "textContent")#此处是视频总时间的xpath
        #print("播放时间:", vedio_stat_time, '结束时间:', vedio_end_time)
        time.sleep(1)
        return vedio_stat_time, vedio_end_time
    except:
        pass
 
 
#翻页,若没有下一页则结束
def vedio_if2(vedio_stat_time, vedio_end_time):
        try:
            browser.switch_to.default_content()
            time.sleep(4)
            #此处是下一页按钮的xpath
            browser.find_element_by_xpath('//*[@id="mainid"]/div[1]/div[2]').click()
            time.sleep(4)
            print("----------没有第二节课了,即将进入下一页----------")
            button()
        except Exception:
            print('---------结束----------')
 
if __name__ == '__main__':
    login()
    html_1()
    html_2()
    button()
    time_tuple=vedio_if()
    while time_tuple[0]!=time_tuple[1]:
        time_tuple=vedio_if()
        if time_tuple[0]==time_tuple[1]:
            vedio_if2(time_tuple[0],time_tuple[1])
            time_tuple=vedio_if()#说实话,我感觉这里的结构有问题,但我实在不想改了,有兴趣的人可以自己改一下

友情提示:如要使用的话,代码中的URL都需要更改

更多资源请访问:blog.123wk.top

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 共1条
123网课的头像-网课代刷

昵称

取消
昵称表情代码图片