使用python的selenium库刷超星网课

网课很多看不完呀

所以动手做了一个基础的自动答题和下一节的程序

用到了python 3

selenium

Chrome

如何自动化Chrome?https://www.cnblogs.com/eternal1025/p/8880245.html

配置好之后上代码

源码下载地址  https://github.com/zlaiyyf/ChaoXIing_seleniumm

时间紧张所以嘛bug就。。。。

 coding:utf-8
from selenium import webdriver
# 显示等待
from selenium.webdriver.support.wait import WebDriverWait
# 启动参数
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import UnexpectedAlertPresentException,TimeoutException
from scrip import click
import time

class Chaoxing():

    def __init__(self,password,username):
        """
        暂时不考虑其他学校的

        :param password:
        :param username:
        """

        chrome_opt =  Options()  # 创建参数设置对象.
        # chrome_opt.add_argument(‘–start-maximized‘)  # 设置浏览器窗口大小.

        chrome_opt.add_argument(‘--disable-infobars‘)
        # chrome_opt.add_argument("-–start-maximized")

        chrome_opt.add_experimental_option(‘excludeSwitches‘, [‘enable-automation‘])
        self.browser = webdriver.Chrome(chrome_options=chrome_opt)
        self.username = username
        self.password = password

    def land(self):
        """
        登陆
        :return:
        """

        self.browser.get(‘http://sxu.fanya.chaoxing.com/portal‘)
        # self.browser.find_element_by_xpath(‘//input[@value= "登录"]‘).click()
        WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath(‘//input[@value= "登录"]‘)).click()
        # ActionChains(driver).click(click_btn)
        WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath(‘//input[@id="unameId"]‘)).send_keys(self.username)
        WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath(‘//input[@id="passwordId"]‘)).send_keys(self.password)
        print(‘输入账号完成{}‘.format(self.username))
        time.sleep(10)

        WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath(‘//input[@value= "登录"]‘)).click()

    def find_course(self):
        """
        发现课程
        :return:
        """
        self.browser.switch_to.frame(‘frame_content‘)
        self.browser.find_elements_by_xpath(‘//li[@style="position:relative"]‘)
        course_name = self.browser.find_elements_by_xpath(‘//h3[@class="clearfix"]‘)
        click.click_couse(course_name,‘创业创新领导力‘)

        windows = self.browser.window_handles
        self.browser.switch_to.window(windows[-1])
        self.couse()
    def couse(self):
        """
        进入课程
        :return:
        """
        class_num = -1
        while True:

            time.sleep(2)
            class_num = class_num + 1
            class_name_list = self.browser.find_elements_by_xpath(‘//div[@class="leveltwo"]‘)
            if class_num == len(class_name_list):
                break
            else:
                print(class_num)
                class_name_num = self.browser.find_elements_by_xpath(‘//div[@class="leveltwo"]‘)[class_num].text
                print(class_name_num.split(‘\n‘))
                if ‘1‘ == class_name_num.split(‘\n‘)[1]:
                    continue
                class_name_tag = self.browser.find_elements_by_xpath(‘//span[@class="articlename"]‘)[class_num]
                class_name = class_name_tag.text
                class_name_tag.click()

                print(‘正在点击{}‘.format(class_name))

                time.sleep(2)
                self.view(class_name=class_name)
                continue
    def view(self,class_name):
        """
        看视频
        :param class_name:
        :return:
        """
        # try:
        self.browser.find_element_by_xpath(‘//span[@title="视频"]‘).click()
        self.browser.switch_to.frame("iframe")
        time.sleep(5)
        self.browser.switch_to.frame(self.browser.find_element_by_xpath(‘//iframe[@class="ans-attach-online ans-insertvideo-online"]‘))
        WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath(‘//div[@id="video"]‘)).click()

        view_tag = self.browser.find_element_by_xpath(‘//div[@id="video"]‘)
        ActionChains(self.browser).move_to_element(view_tag).perform()
        while   True:
            time.sleep(2)

            if self.view_percentage() == ‘200‘ :
                self.browser.switch_to.default_content()
                self.browser.find_element_by_xpath(‘//a[contains(text(), "回到课程")]‘).click()
                break

    def view_percentage(self):
        """"
                  检查是否看完
        """
        # total_duration = self.browser.find_element_by_xpath(‘//span[@class="vjs-duration-display"]‘).text
        # current_duration = self.browser.find_element_by_xpath(‘//span[@class="vjs-current-time-display"]‘).text
        view_percentage_tag = self.browser.find_element_by_xpath(‘//div[@class="vjs-play-progress vjs-slider-bar"]‘)
        view_percentage = view_percentage_tag.get_attribute(‘style‘)
        print(‘当前进度‘+view_percentage)
        self.is_exist_problem()
        """"
        检查是否看完
        """
        if ‘100%‘ in view_percentage :
            return ‘200‘

    def is_exist_problem(self):
        try:
            problem_tag_style = WebDriverWait(self.browser, 30, 0.2).until(
                lambda x: x.find_element_by_xpath(‘//div[@id="ext-comp-1035"]‘)).get_attribute(‘style‘)

            if problem_tag_style == ‘overflow: auto;‘:
                print(‘有题目‘)
                input_tag_list = self.browser.find_elements_by_xpath(‘//input‘)
                for input_tag in input_tag_list:
                    input_tag.click()
                    self.browser.find_element_by_xpath(‘//div[@class="ans-videoquiz-submit"]‘).click()
                    time.sleep(2)
                    if EC.alert_is_present()(self.browser):
                        self.browser.switch_to.alert.accept()
                    else:
                        break
            else:
                pass
        except UnexpectedAlertPresentException:
            print(‘alert出错‘)
            self.browser.switch_to.alert.accept()
        except TimeoutException:
            print(‘TimeoutException‘)
            pass

原文地址:https://www.cnblogs.com/zhaolinag/p/11774388.html

时间: 2024-07-31 06:28:19

使用python的selenium库刷超星网课的相关文章

python利用selenium库识别点触验证码

利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路) 一.超级鹰注册:超级鹰入口 1.首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分,基本上就够学习使用了.如果想一直用可以用,可以充值,不是很贵. 2.下载超级鹰的python库代码.代码 3.然后有测试案例,自己可以试着跑一跑代码. 二.使用selenium库来识别点触式验证码: 1.首先是找一个使用点触式二维码的网站:(这个真的是比较难找了,由于静谧大大书上的网站被封了,我找

python beautiful soup库的超详细用法

原文地址https://blog.csdn.net/love666666shen/article/details/77512353 参考文章https://cuiqingcai.com/1319.html 1. Beautiful Soup 简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要

浅谈python中selenium库调动webdriver驱动浏览器的实现原理

最近学web自动化时用到selenium库,感觉很神奇,遂琢磨了一下,写了点心得. 当我们输入以下三行代码并执行时,会发现新打开了一个浏览器窗口并访问了百度首页,然而这是怎么做到的呢? 1 from selenium import webdriver 2 driver = webdriver.Chrome() 3 driver.get('http://www.baidu.com') 首先我们来看一下selenium库的结构: 很显然,selenium就是一个软件包,里面有两个一级子包,commo

基于python和selenium的刷网课的代码

主要是通过使用selenium的查找定位来实现,同时通过time.sleep来控制时间网址:http://hrbj.21tb.com里面的课程挺不错的,都是一些别的网站上的付费教程.(不得不感慨,铁路系统待遇就是好呀.别人花钱想买的和想求的资源,免费看都懒得看)这个刷课主要是替代人的操作.没用什么脚本或者加速什么的,全部都是实打实的播放.打算挂机刷网课的小伙伴们,可以参考一下! ? from selenium import webdriverimport jsonimport timefrom

完全机器模拟浏览器操作自动刷网课!不怕被封!!-----python基于selenium实现超星学习通刷视频网课

原谅我这个标题党,对叭起 最近没事觉得网课恶心人,“你们学计算机的,随便写个程序玩玩,很容易哒”------语出高数老师,于是我就“随便”写了个刷网课的程序,没什么nb算法,请各路大神指教 原谅我的个别函数名和变量名用了拼音,别喷我low呜呜呜(其实就是懒,打拼音顺手了,这是个坏习惯,大家别学我) 以上的都是废话 需要注意的是要安装一个浏览器驱动,去网上下载就好了,把地址添加在第四行那里,我代码里面的是谷歌浏览器,如果想要用其他的浏览器代码换成相应的就行 然后要自己在终端输入自己的账号.密码,还

为采集动态网页安装和测试Python Selenium库

1. 引言 上一篇<为编写网络爬虫程序安装Python3.5>中测试小例子对静态网页做了一个简单的采集程序,而动态网页因为需要动态加载js获取数据,所以使用urllib直接openurl已经不能满足采集的需求了.这里我们使用selenium库,通过它我们可以很简单的使用浏览器来为我们加载动态内容,从而获取采集结果. 在很多案例中,Selenium与PhantomJS搭配采集动态网页内容(可以参看我以前发表的案例文章),直接与Firefox或者Chrome搭配,可以应对一些更加复杂的采集情形,比

python爬虫笔记----4.Selenium库(自动化库)

4.Selenium库 (自动化测试工具,支持多种浏览器,爬虫主要解决js渲染的问题) pip install selenium 基本使用 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_condition

python selenium找不到selenium库

初学者在做python自动化时,经常会遇到找不到库情形,如下图所示 方法一: 在pycharm中,通过File->settings ->Project Interpreter 选择“+”号 安装selenium库 装完之后,项目解释器下变会出现selenium 此时代码中红色提示消失,可以正常使用 方法二:直接修改Project Interpreter 为python目录 原文地址:https://www.cnblogs.com/tim2016/p/10804376.html

Python的常用库

读者您好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都应该有它. Scrapy.如果你从事爬虫相关的工作,那么这个库也是必不可少的.用过它之后你就不会再想用别的同类库了. wxPython.Python的一个GUI(图形用户界面)工具.我主要用它替代tkinter.你一定会爱上它的. Pillow.它是PIL(Python图形库)的一个友好分支.对于用