美文网首页
selenium是个好东西。

selenium是个好东西。

作者: 麦睿蔻 | 来源:发表于2019-08-26 15:53 被阅读0次

在CSDN网上找了段代码,据说可以自动化强国学习,初步调试可以运行,只是半自动的,需要扫描二维码还有点击一次播放键,实用意义并不大,不过里面的很多代码还是值得好好学习一下的。

# -*- coding: utf-8 -*-


from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get('https://pc.xuexi.cn/points/login.html')
driver.maximize_window()
driver.execute_script("var q=document.documentElement.scrollTop=1000")
time.sleep(10)
driver.get('https://www.xuexi.cn/')
print("模拟登录完毕\n")

driver.get("https://www.xuexi.cn/d05cad69216e688d304bb91ef3aac4c6/9a3668c13f6e303932b5e0e100fc248b.html")
articles = driver.find_elements_by_xpath(".//*[@class='text-link-item-title']")
for index, article in enumerate(articles):
    if index > 7:
        break
    article.click()
    all_handles = driver.window_handles
    driver.switch_to_window(all_handles[-1])
    driver.get(driver.current_url)
    for i in range(0, 2000, 100):

        js_code = "var q=document.documentElement.scrollTop=" + str(i)
        driver.execute_script(js_code)
        time.sleep(5)
    for i in range(2000, 0, -100):
        js_code = "var q=document.documentElement.scrollTop=" + str(i)
        driver.execute_script(js_code)
        time.sleep(5)
    time.sleep(80)
    driver.close()
    driver.switch_to_window(all_handles[0])
print("阅读文章完毕\n")


driver.get("https://www.xuexi.cn/a191dbc3067d516c3e2e17e2e08953d6/b87d700beee2c44826a9202c75d18c85.html?pageNumber=39")
videos = driver.find_elements_by_xpath("//div[@id='Ck3ln2wlyg3k00']")
spend_time = 0

for i, video in enumerate(videos):
    if i > 6:
        break
    video.click()
    all_handles = driver.window_handles
    driver.switch_to_window(all_handles[-1])
    driver.get(driver.current_url)

# 点击播放
    driver.find_element_by_xpath("//div[@class='outter']").click()
        # 获取视频时长
    video_duration_str = driver.find_element_by_xpath("//span[@class='duration']").get_attribute('innerText')
    video_duration = int(video_duration_str.split(':')[0]) * 60 + int(video_duration_str.split(':')[1])
        # 保持学习,直到视频结束
    time.sleep(video_duration + 3)
    spend_time += video_duration + 3
    driver.close()
    driver.switch_to_window(all_handles[0])

    # if spend_time < 3010:
    #     browser.get(LONG_VIDEO_LINK)
    #     browser.execute_script("var q=document.documentElement.scrollTop=850")
    #     try:
    #         browser.find_element_by_xpath("//div[@class='outter']").click()
    #     except:
    #         pass
    #
    #     # 观看剩下的时间
    #     time.sleep(3010 - spend_time)
driver.get("https://www.xuexi.cn/8e35a343fca20ee32c79d67e35dfca90/7f9f27c65e84e71e1b7189b7132b4710.html")
time.sleep(3010 - spend_time)
print("播放视频完毕\n")

相关文章

网友评论

      本文标题:selenium是个好东西。

      本文链接:https://www.haomeiwen.com/subject/vqcdectx.html