分享

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

 wenxuefeng360 2022-07-21 发布于四川

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])        
                
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134

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

版权声明:本文为weixin_44912902原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多