python脚本模拟浏览器实现学习通自动刷网课

1.使用selenium库

2.下载谷歌驱动(驱动版本最好是跟本地谷歌版本差不多)

整体代码如下:

  • 判断是否为视频或者文档 是视频就播放 是文档就下一页操作
#判断是否为视频 是就播放 不是就下一页操作
from selenium import webdriver
import time

browser = webdriver.Chrome(executable_path="D:\google\chromedriver.exe")
url='http://passport2.chaoxing.com/wunitlogin?refer=http%3A%2F%2Fi.mooc.chaoxing.com'
browser.get(url)


#学校 学号 密码 验证码登陆
def login():
    inp_1 = input("请输入学校:")
    inp_2 = input("请输入学号:")
    inp_3 = input("请输入密码:")
    inp_4 = input("请输入验证码:")
    school = browser.find_element_by_id("FidName")
    username = browser.find_element_by_id("idNumber")
    password = browser.find_element_by_id("pwd")
    verycode = browser.find_element_by_id("numcode")
    school.send_keys(inp_1)
    username.send_keys(inp_2)
    password.send_keys(inp_3)
    verycode.send_keys(inp_4)
    browser.find_element_by_xpath('//*[@id="userLogin"]/div/a').click()
    print("----------正在登陆中----------")




# 进入主页 开始选择课程 xpath匹配
def html_1():
    time.sleep(2)

    browser.find_element_by_xpath('//*[@id="zne_kc_icon"]').click()     #课程选项的xpath
    print("----------进入课程----------")
    time.sleep(2)
    browser.switch_to.frame("frame_content")

    #选择我需要学的课的xpath
    browser.find_element_by_xpath('/html/body/div/div[2]/div[2]/ul/li[6]/div[2]/h3/a').click()  #课程
    print("----------已进入所点击课程----------")

def html_2():
    #浏览器标签页跳转设置
    browser.switch_to.window(browser.window_handles[1])

    #进入课程中的第一个小节
    time.sleep(1)
    browser.find_element_by_xpath('/html/body/div[5]/div[1]/div[2]/div[3]/div[1]/div[1]/h3/span[3]/a').click()  #开始小节


# button模拟点击播放
def button():
    time.sleep(1)
    try:
        #进入frame播放框架
        browser.switch_to.frame("iframe")
        browser.switch_to.frame(0)
        time.sleep(2)                              #此处等待需要就一些就不会报错终止程序
        browser.find_element_by_xpath('//*[@id="video"]/button').click()
        print('----------等待播放----------')
        time.sleep(2)
        print("----------课程正在播放中----------")

    except:
        # 回到主框架
        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[4]/div[2]/span[2]').get_attribute("textContent")
        vedio_end_time = browser.find_element_by_xpath('//*[@id="video"]/div[4]/div[4]/span[2]').get_attribute("textContent")
        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):
    if vedio_stat_time ==vedio_end_time:
        try:
            browser.switch_to.default_content()
            browser.switch_to.frame("iframe")
            browser.switch_to.frame(1)
            browser.find_element_by_xpath("//*[@id='video']/button").click()
            time.sleep(2)

        except:
            print("----------没有第二节课了,即将进入下一页----------")

def next_start(vedio_stat_time,vedio_end_time):
    if vedio_stat_time==vedio_end_time:
        try:
            browser.switch_to.default_content()
            print("----------开始点下一页----------")
            time.sleep(1)
            browser.find_element_by_xpath('//*[@id="mainid"]/div[1]/div[2]').click()

            time.sleep(2)

        except:
            print("----------结束----------")


if __name__=='__main__':
    login()
    html_1()
    html_2()

    while True:
        button()
        time_tuple = vedio_if()
        while time_tuple[0] != time_tuple[1]:
            time_tuple = vedio_if()
            try:
                vedio_if2(time_tuple[0],time_tuple[1])
                if time_tuple[0] ==time_tuple[1]:
                    print("----------开始测试第二节课时间----------")
                    time_tuple_2 = vedio_if()
                    while time_tuple_2[0] !=time_tuple_2[1]:
                        time_tuple_2 = vedio_if()
                        next_start(time_tuple_2[0],time_tuple_2[1])
            except:
                next_start(time_tuple[0],time_tuple[1])        
                

源代码参照文章:https://www.cnblogs.com/xhfzjbs/p/12028948.html

发布了5 篇原创文章 · 获赞 12 · 访问量 5543

猜你喜欢

转载自blog.csdn.net/weixin_44912902/article/details/104576842