selenium自动翻页

 



前言

生活中,很多同学由于看课而烦恼。

简单学习了selenium后,我觉得可以改变一下现状。

今天我们使用selenium+chrome浏览器来完成自动翻页,解放双手。

 

目录

前言

一、selenium是什么?

二、配置环境

1.安装Chrome浏览器

2.安装selenium

3.检测成功

三、使用步骤

1.引入库

2.用户输入数据

3.打开网页,输入数据,尝试登陆

4.找到要看的课程

5.不同的课的观看方法(如果有其他的课,可以加入不同函数)

6.程序判断是否观看,未观看执行观看程序

四、完整代码

总结


 

 

提示:以下是本篇文章正文内容,下面案例可供参考



一、selenium是什么?

Selenium是一个免费的(开源)自动化测试组件,适用于跨不同浏览器和平台的Web应用程序。Selenium侧重于自动化基于Web的应用程序。可以代替人们完成在浏览器的操作。

二、配置环境

1.安装Chrome浏览器

首先我们安装Chrome浏览器,位置默认或者自己定义2021091321244762.png

下载对应的驱动,选择自己的版本下载。

2021091321255818.png20210913212605707.png

下载后解压将chromedriver.exe文件放入Chrome的安装位置。

20210913212700568.png

 watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAcXFfMjMxNDc2OTUzOA==,size_20,color_FFFFFF,t_70,g_se,x_16

打开计算机的环境变量,将Chrome安装位置设置成环境变量,我这里用默认位置

C:Program FilesGoogleChromeApplication演示。

20210913213224626.png

 右击 此电脑 ,点击属性

watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAcXFfMjMxNDc2OTUzOA==,size_11,color_FFFFFF,t_70,g_se,x_16

 watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAcXFfMjMxNDc2OTUzOA==,size_15,color_FFFFFF,t_70,g_se,x_16

 

 watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAcXFfMjMxNDc2OTUzOA==,size_20,color_FFFFFF,t_70,g_se,x_16

 watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAcXFfMjMxNDc2OTUzOA==,size_17,color_FFFFFF,t_70,g_se,x_16

 到这里,我们环境的Chrome浏览器的配置就已经差不多了。

 

2.安装selenium

我这里使用的是pycharm安装selenium。点击左上角File->settings->搜索:Python Interpreter->点击左下方的+号->搜索:selenium->点击左下角Install Packge

如果速度非常慢,点击Manage Repositories,点击+号,输入

https://pypi.tuna.tsinghua.edu.cn/simple/

返回确认就可以了

3.检测成功

输入下面代码,如果成功进入百度,则进入下一步

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoption = Options()option.add_experimental_option('excludeSwitches', ['enable-automation'])web = webdriver.Chrome(executable_path=r"C:Program FilesGoogleChromeApplicationchromedriver.exe", options=option)  # 使用浏览器驱动web.get('https://www.baidu.com/')



三、使用步骤



1.引入库

from selenium import webdriverimport timefrom selenium.webdriver.chrome.options import Optionsimport sys



2.用户输入数据

phonenum = input('请输入账号')password = input('请输入密码')km = input('请输入学科')

3.打开网页,输入数据,尝试登陆

web.get('http://mooc2-ans.chaoxing.com/visit/interaction')phone = web.find_element_by_id('phone')  ##找到手机号输入位置pwd = web.find_element_by_id('pwd')  ##找到密码位置login = web.find_element_by_id('loginBtn')  ##找到登录位置# 登录phone.send_keys(phonenum)  ##输入用户手机号pwd.send_keys(password)  ##输入用户密码login.click()  ##点击登录time.sleep(2)

4.找到课程

#寻找需要刷的课程lists = web.find_elements_by_tag_name('a')for a in lists:    if a.text == km:  ##km为用户输入的科目        a.click()time.sleep(2)# 切换句柄windows = web.window_handlesweb.switch_to.window(windows[-1])  ##需要换到界面二num = 0lists1 = web.find_elements_by_tag_name('a')  ##登录后,查看网络源代码后,发现a标签的20个为第一课for b in lists1:    num = num + 1    if num > 20:        b.click()        break

5.不同的课的观看方法(如果有其他的课,可以加入不同函数)

class watch():    def __init__(self, n):        self.n = n    def ppt(self):  ##PPt观看        web.switch_to.frame(self.n)        num = web.find_element_by_class_name('all').text        for i in range(int(num)-1):            web.find_element_by_class_name('nextBtn').click()            time.sleep(0.2)        web.switch_to.parent_frame()    def listen(self):  ##音频观看        web.switch_to.frame(self.n)        web.find_element_by_xpath('//*[@id="audio"]/div[5]/button').click()        web.switch_to.parent_frame()    def video(self):  ##视频观看        web.switch_to.frame(self.n)        web.find_element_by_xpath('//*[@id="video"]/button').click()        web.switch_to.parent_frame()

6.程序判断是否观看,未观看执行观看程序

class page():    def __init__(self):        self.nums = len(web.find_elements_by_class_name('ncells'))        self.num = 1    def next(self):        web.switch_to.default_content()        go1 = web.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div[2]')        go1.click()        time.sleep(2)        self.num += 1        if self.num == self.nums:            web.quit()            sys.exit()    def classes(self):        if web.find_elements_by_tag_name('iframe') == []:            pass        else:            iframe = web.find_element_by_tag_name('iframe')            web.switch_to.frame(iframe)            i = web.find_elements_by_class_name('ans-attach-ct')            m = web.find_elements_by_tag_name('iframe')            for self.a, self.b in zip(i, m):                j = self.b.get_attribute('class')                if j == "ans-attach-online insertdoc-online-ppt" or j == "ans-attach-online insertdoc-online-pdf":                    print('检测到PPT文件   ', end='')                    if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                        print('检测到已经完成,却换到下一个')                    else:                        print('检测到未完成,开始完成')                        watch(self.b).ppt()                        while 1:                            if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                                break                elif j == 'ans-attach-online ans-insertvideo-online':                    print('检测到视频文件   ', end='')                    if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                        print('检测到已经完成,切换到下一个')                    else:                        print('检测到未完成,开始完成')                        watch(self.b).video()                        while 1:                            if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                                break                elif j == 'ans-attach-online ans-insertaudio':                    print('检测到音频文件   ', end='')                    if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                        print('检测到已经完成,切换到下一个')                    else:                        print('检测到未完成,开始完成')                        watch(self.b).listen()                        while 1:                            if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                                break                else:                    pass        self.next()        self.classes()

四、完整代码

from selenium import webdriverimport timefrom selenium.webdriver.chrome.options import Optionsimport sysphonenum = input('请输入账号')password = input('请输入密码')km = input('请输入学科')option = Options()option.add_experimental_option('excludeSwitches', ['enable-automation'])web = webdriver.Chrome(executable_path=r"C:Program FilesGoogleChromeApplicationchromedriver.exe", options=option)  # 使用浏览器驱动web.get('http://mooc2-ans.chaoxing.com/visit/interaction')phone = web.find_element_by_id('phone')pwd = web.find_element_by_id('pwd')login = web.find_element_by_id('loginBtn')# 登录phone.send_keys(phonenum)pwd.send_keys(password)login.click()time.sleep(2)#寻找需要刷的课程lists = web.find_elements_by_tag_name('a')for a in lists:    if a.text == km:        a.click()time.sleep(2)# 切换句柄windows = web.window_handlesweb.switch_to.window(windows[-1])go0 = web.find_element_by_xpath('/html/body/div[1]/div[1]/div/a')#添加两句        go0.click()num = 0lists1 = web.find_elements_by_tag_name('a')for b in lists1:    num = num + 1    if num > 20:        b.click()        breakclass watch():    def __init__(self, n):        self.n = n    def ppt(self):        web.switch_to.frame(self.n)        num = web.find_element_by_class_name('all').text        for i in range(int(num)-1):            web.find_element_by_class_name('nextBtn').click()            time.sleep(0.2)        web.switch_to.parent_frame()    def listen(self):        web.switch_to.frame(self.n)        web.find_element_by_xpath('//*[@id="audio"]/div[5]/button').click()        web.switch_to.parent_frame()    def video(self):        web.switch_to.frame(self.n)        web.find_element_by_xpath('//*[@id="video"]/button').click()        web.switch_to.parent_frame()class page():    def __init__(self):        self.nums = len(web.find_elements_by_class_name('ncells'))        self.num = 1    def next(self):        web.switch_to.default_content()        go1 = web.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div[2]')        go1.click()        time.sleep(2)        self.num += 1        if self.num == self.nums:            web.quit()            sys.exit()    def classes(self):        if web.find_elements_by_tag_name('iframe') == []:            pass        else:            iframe = web.find_element_by_tag_name('iframe')            web.switch_to.frame(iframe)            i = web.find_elements_by_class_name('ans-attach-ct')            m = web.find_elements_by_tag_name('iframe')            for self.a, self.b in zip(i, m):                j = self.b.get_attribute('class')                if j == "ans-attach-online insertdoc-online-ppt" or j == "ans-attach-online insertdoc-online-pdf":                    print('检测到PPT文件   ', end='')                    if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                        print('检测到已经完成,却换到下一个')                    else:                        print('检测到未完成,开始完成')                        watch(self.b).ppt()                        while 1:                            if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                                break                elif j == 'ans-attach-online ans-insertvideo-online':                    print('检测到视频文件   ', end='')                    if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                        print('检测到已经完成,切换到下一个')                    else:                        print('检测到未完成,开始完成')                        watch(self.b).video()                        while 1:                            if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                                break                elif j == 'ans-attach-online ans-insertaudio':                    print('检测到音频文件   ', end='')                    if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                        print('检测到已经完成,切换到下一个')                    else:                        print('检测到未完成,开始完成')                        watch(self.b).listen()                        while 1:                            if self.a.get_attribute('class') == 'ans-attach-ct ans-job-finished':                                break                else:                    pass        self.next()        self.classes()pages = page()pages.classes()


总结

希望文章可以对一些好兄弟有帮助。

很少写文章,有错误的地方,还望大佬指出。

 

 

 

 

  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贪玩的学渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值