关于学习的一些研究

一、所需技术(模块/包)

import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select

使用selenium进行自动化操作

二、分析具体页面

在分析具体页面之前,我们需要获取大学习的具体链接。

1.iframe

学过python爬虫的应该都知道有一个案例:登录空间,和这个是类似的。

就是说一个网页中内嵌了另外的一个网页,也就是有个iframe。

学习也是如此 

5d2d4183557f4604b3355fdfd28ab395.png

因此我们接下来的操作都是争对这个iframe页面进行的。

url = 'https://h5.cyol.com/special/daxuexi/byw1m1kn1s/m.html'
driver = webdriver.Chrome()
driver.get(url)
# 窗口最大化
driver.maximize_window()
time.sleep(1)
# 进入iframe
driver.switch_to.frame(0)

首先驱动浏览器打开网址,然后进入iframe方便后续操作!!

2.select(下拉框处理模块)

在学习前,会显示需要选择省市的两个下拉框,不过不要慌,Selenium给我们提供了专门的Select(下拉框处理模块)

我们可以根据许多方面(如下)选择相应的内容

这里需要注意下第一个是“请选择”,具体省份是从第二个开始的

①根据索引index

Select(driver.find_element_by_id('province')).select_by_index(11)

②根据value

Select(driver.find_element_by_id('province')).select_by_value('11')

③根据内容

Select(driver.find_element_by_css_selector('#province')).select_by_visible_text('浙江省')

以上三种都需要先定位到元素,即通过

driver.find_element_by_方法

最后再进行点击确认按钮

driver.find_element_by_xpath('/html/body/div[2]/div[3]').click()

接下来出现了开始学习的按钮

time.sleep(3)
driver.execute_script("document.querySelector('video').playbackRate = 15.0;")
driver.find_element_by_css_selector('body > div.section0.topindex > div.start_btn').click()

笔者在这里是先进行了视频倍速,再开始点击开始学习按钮,大家可以自行去试试看反过来是否可行。

接下来的一步是重点

3.倍速播放视频(重点)

driver.execute_script("document.querySelector('video').playbackRate = 15.0;")

该代码在python给网页执行js代码,实现了视频倍速播放的功能 

该代码相当于在网页的console控制台执行 

document.querySelector('video').playbackRate = 15.0;

注:Chrome的最高倍速好像是16,高过这个值就会报错

四、自动做题

1.思路

由于XX大学习的题目不需要管对错,为了方便,选A即可,你也可以用随机数进行随机选择。

2.判断题目是否出现

经过笔者测试(只测了两次的大学习),猜测每次大学习只有两个题目,而且是上图的section 1和section 2

当出现题目时,这两个div的class会发生变化,右键copy对应的selector,得到css_selector

body > div.section1.topindex1
body > div.section2.topindex1

因此我们需要判断是否会出现题目,如果出现题目,进行答题,否则继续看视频

这里笔者采用的时while True,try except的方式进行判断。

3.答题

# 选项
# body > div.section1.topindex1 > div.w1.option
# 确认按钮
# body > div.section1.topindex1 > div.button
# 继续听讲按钮
# body > div.section1.topindex1 > div.continue

以上是copy的selector


 注:继续听讲按钮在你答题后不是立刻出现的,你需要设置时间等它出现,才能进行点击事件。

 答题代码块如下

while True:
    try:
        # 是否出现题目
        driver.find_element_by_css_selector('body > div.section1.topindex1')
        time.sleep(0.1)
        # 选A
        driver.find_element_by_css_selector('body > div.section1.topindex1 > div.w1.option').click()
        time.sleep(0.1)
        # 确认按钮
        driver.find_element_by_css_selector('body > div.section1.topindex1 > div.button').click()
        time.sleep(2)
        #
        driver.find_element_by_css_selector('body > div.section1.topindex1 > div.continue').click()
    except:
        pass
    try:
        driver.find_element_by_css_selector('body > div.section2.topindex1')
        time.sleep(0.1)
        driver.find_element_by_css_selector('body > div.section2.topindex1 > div.w1.option').click()
        time.sleep(0.1)
        driver.find_element_by_css_selector('body > div.section2.topindex1 > div.button').click()
        time.sleep(2)
        driver.find_element_by_css_selector('body > div.section2.topindex1 > div.continue').click()
    except:
        pass

五、截图(保存图片)、关闭浏览器

# 需要截图的时候
# body > div.section3.topindex1

这是copy的selector

这和上文的自动答题一样,在出现该界面时,这个div的class会从section 3变成section3 topindex1

因此也是采取try except的方式,即如果出现该界面(播放完视频后)的时候,过一秒钟后截图(最后一个应该是动画,立即截图会使图片太淡),接下来将截取的图片以时间命名保存在桌面,最后关闭浏览器。

    try:
        if driver.find_element_by_css_selector('body > div.section3.topindex1'):
            time.sleep(1)
            a = time.time()
            driver.get_screenshot_as_file(r'C:\Users\knighthood\OneDrive\桌面\{}.png'.format(a))
            break
    except:
        pass

这里笔者将try except和上面的两个try except并列,并将关闭浏览器的代码放在了最后。

六、全部代码展示

import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
# url = 'https://h5.cyol.com/special/daxuexi/bq2hwo9h7e/index.html'
# url = input('请输入网址:')
url = 'https://h5.cyol.com/special/daxuexi/byw1m1kn1s/m.html'
driver = webdriver.Chrome()
driver.get(url)
# 窗口最大化
driver.maximize_window()
time.sleep(1)
# 进入iframe
driver.switch_to.frame(0)
# driver.find_element_by_css_selector('#province').click()
# Select(driver.find_element_by_id('province')).select_by_index(11)
# Select(driver.find_element_by_id('province')).select_by_value('11')
time.sleep(1)
Select(driver.find_element_by_css_selector('#province')).select_by_visible_text('浙江省')
# time.sleep(1)
Select(driver.find_element_by_css_selector('#city')).select_by_visible_text('绍兴市')
# time.sleep(1)
driver.find_element_by_xpath('/html/body/div[2]/div[3]').click()
# driver.find_element_by_css_selector('body > div.section00 > div.sure').click()
# 倍速
time.sleep(3)
driver.execute_script("document.querySelector('video').playbackRate = 15.0;")
driver.find_element_by_css_selector('body > div.section0.topindex > div.start_btn').click()

while True:
    try:
        # 是否出现题目
        driver.find_element_by_css_selector('body > div.section1.topindex1')
        time.sleep(0.1)
        # 选A
        driver.find_element_by_css_selector('body > div.section1.topindex1 > div.w1.option').click()
        time.sleep(0.1)
        # 确认按钮
        driver.find_element_by_css_selector('body > div.section1.topindex1 > div.button').click()
        time.sleep(2)
        #
        driver.find_element_by_css_selector('body > div.section1.topindex1 > div.continue').click()
    except:
        pass
    try:
        driver.find_element_by_css_selector('body > div.section2.topindex1')
        time.sleep(0.1)
        driver.find_element_by_css_selector('body > div.section2.topindex1 > div.w1.option').click()
        time.sleep(0.1)
        driver.find_element_by_css_selector('body > div.section2.topindex1 > div.button').click()
        time.sleep(2)
        driver.find_element_by_css_selector('body > div.section2.topindex1 > div.continue').click()
    except:
        pass
    try:
        if driver.find_element_by_css_selector('body > div.section3.topindex1'):
            time.sleep(1)
            a = time.time()
            driver.get_screenshot_as_file(r'C:\Users\knighthood\OneDrive\桌面\{}.png'.format(a))
            break
    except:
        pass
driver.close()

七、总结

在网上没有看到相关的内容,所以就自己写了这个代码。

如需转载,请注明!!

1.在使用过程中,如果倍速过快,会导致视频卡顿

2.大家如果在使用过程中出现报错,请尝试用一下try except语句,因为笔者只测试了两期大学习!

3.while true作为一个死循环,一直在运行,会导致资源的浪费,如果大家有其他判断是否出现问题的想法,麻烦私信我一下

4.笔者认为复制问卷星的链接还是最复杂的,如果大家有其他想法的可以私聊哦!!❤

5.以后会更新更完善的博客,敬请期待!

猜你喜欢

转载自blog.csdn.net/knighthood2001/article/details/121168527