【军事理论课】第二代脚本

发布于 2019-10-16  111 次阅读


以下是第二代的代码

  • 首先要使用pip 安装 selenium
  • 需要下载chromedriver,并且将其添加到PATH中
  • 第二代码只需要在main函数中修改login的用户名与密码就可以了
from selenium import webdriver
import time
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
orange_url = []
def login(user,passwd):
    global driver
    driver.get("https://passport2.chaoxing.com/login?fid=183&refer=http://i.mooc.chaoxing.com/space/index.shtml")
    driver.find_element_by_xpath('//*[@id="unameId"]').send_keys(user)
    driver.find_element_by_xpath('//*[@id="passwordId"]').send_keys(passwd)
    codes = input("Please input the verify codes:")
    driver.find_element_by_xpath('//*[@id="numcode"]').send_keys(codes)
    driver.find_element_by_xpath('//*[@id="form"]/table/tbody/tr[7]/td[2]/label/input').click()
    iframe = driver.find_element_by_xpath('//*[@id="frame_content"]')
    driver.switch_to_frame(iframe)
    driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/ul/li[1]/div[2]/h3/a').click()
    driver.switch_to_window(driver.window_handles[0])
    driver.close()
    driver.switch_to_window(driver.window_handles[0])

def detect_finshed():
    global driver
    class_attribute = driver.find_element_by_id('ext-gen1039').get_attribute('class')
    return class_attribute

def change_frame():
    global driver
    iframe = driver.find_element_by_xpath('//*[@id="iframe"]')
    driver.switch_to_frame(iframe)
    time.sleep(4)
    d = driver.find_element_by_id('ext-gen1039').get_attribute('class')
    if "finished" in d:
        print("当前视频章节已完成")
    else:
        iframe_2 = driver.find_element_by_xpath('//*[@id="ext-gen1039"]/iframe')
        time.sleep(4)
        driver.switch_to_frame(iframe_2)
        # video = driver.find_element_by_xpath('//*[@id="video_html5_api"]')
        # driver.execute_script("return arguments[0].play()", video)
        t1 = driver.find_element_by_xpath('//*[@id="video"]/div[4]/div[2]/span[2]').text.split(':', 1)
        t1 = int(t1[0]) * 60 + int(t1[-1])
        t2 = driver.find_element_by_xpath('//*[@id="video"]/div[4]/div[4]/span[2]').text.split(':', 1)
        t2 = int(t2[0]) * 60 + int(t2[-1])
        print("剩余时间为:%d s" % (t2 - t1))
        time.sleep(t2 - t1 + 5)

def find_all_orange():
    list = driver.find_elements_by_class_name('orange')
    return list


def main():
    login("我是用户名", "我是密码")
    url_private = driver.current_url
    orange_list = find_all_orange()
    for i in orange_list:
        try:
            orange_url.append(i.find_element_by_xpath("./../../span[2]/a").get_attribute('href'))
        except:
            orange_url.append(i.find_element_by_xpath("./../../span[3]/a").get_attribute('href'))
    for url in orange_url:
        driver.get(url)
        driver.switch_to_window(driver.window_handles[-1])
        time.sleep(3)
        title = driver.find_element_by_xpath('//*[@id="mainid"]/h1').text
        driver.find_element_by_xpath('//*[@id="mainid"]/h1').click()
        print(title,end="")
        iframe = driver.find_element_by_xpath('//*[@id="iframe"]')
        driver.switch_to_frame(iframe)
        d = driver.find_element_by_id('ext-gen1039').get_attribute('class')
        if "finished" in d:
            print("当前视频章节已完成")
            continue
        iframe_2 = driver.find_element_by_xpath('//*[@id="ext-gen1039"]/iframe')
        time.sleep(4)
        driver.switch_to_frame(iframe_2)
        video = driver.find_element_by_xpath('//*[@id="video_html5_api"]')
        driver.execute_script("return arguments[0].play()", video)
        t1 = driver.find_element_by_xpath('//*[@id="video"]/div[4]/div[2]/span[2]').text.split(':', 1)
        t1 = int(t1[0]) * 60 + int(t1[-1])
        t2 = driver.find_element_by_xpath('//*[@id="video"]/div[4]/div[4]/span[2]').text.split(':', 1)
        t2 = int(t2[0]) * 60 + int(t2[-1])
        print("剩余时间为:%d s" % (t2 - t1))
        time.sleep(t2 - t1 + 5)

if __name__ == '__main__':
    main()

你知道雪为什么是白色的吗?因为她忘记了原来的颜色