[随便聊聊]
函授网上视频自动学习python脚本
牛牛先森
发布于 2020-3-26 15:53:34
阅读 4462
查看全部
注册获取更多资料
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Python 是个好东西。
- from selenium import webdriverimport time
- #import pandas as pd
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.wait import WebDriverWait
- from selenium.webdriver.common.action_chains import ActionChains
- from selenium.webdriver.support import expected_conditions as EC
- #import writer_file
- import re
-
-
- class Shuake():
- """docstring for ClassName"""
-
- def __init__(self):
- #super(ClassName, self).__init__()
- # 谷歌浏览器
- self.username = '账号'
- self.userpassword = '密码'
- driver_path = r'F:\project\chromedriver.exe'
- self.driver = webdriver.Chrome(executable_path=driver_path)
-
- def login_jiaoyu(self):
- # 账号密码
- self.driver.get("http://g.cjnep.net/lms/web/default/login")
- # 用户名,密码
- username_input = self.driver.find_element_by_xpath('//*[@id="username"]')
- username_input.click()
- time.sleep(0.5)
- username_input.send_keys(self.username)
- time.sleep(0.5)
- userpassword_input = self.driver.find_element_by_xpath('//*[@id="password"]')
- userpassword_input.send_keys(self.userpassword)
- print('请输入验证码,然后点击登录')
- # 等待检测是否登录成功
- wait = WebDriverWait(self.driver, 100)
- shouye_button = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'active')))
- print("登录成功")
-
- def my_course(self):
- """打开我的课程界面"""
-
- print("打开课程界面")
- self.driver.get("http://g.cjnep.net/lms/web/course/index")
- # 获取网页源码
- my_course_source = self.driver.page_source
- #writer_file.write_file('kk.txt', my_course_source)
- # 获取课程列表
- my_course_source = my_course_source[my_course_source.find('默认;'):]
- # 课程名称
- course_name = re.findall('class="namediv"><span.*">(.*)</span>(.*)</div>', my_course_source)
- # print(course_name)
- # 课程链接
- course_url = re.findall('href="/(.*)">进入学习</a></div>', my_course_source)
- # print(course_url)
- # 学习进度
- course_complete = re.findall(
- '<div class="otherdiv">(.*)<span>(.*)<font>(.*)</font></span> </div>', my_course_source)
- # print(course_complete)
- # 学期 课程名称 主讲老师 完成率 课程链接
- courses_page_list = []
- for x in range(0, len(course_name)):
- term = course_name[x][0]
- name = course_name[x][1]
- url = course_url[x]
- teacher = course_complete[x][0]
- speed = course_complete[x][2]
- print(str(x + 1) + "===" + term + "===" + name + "===" + teacher +
- "===学习完成:" + speed + "===http://g.cjnep.net/" + url)
- courses_page_list.append([term, name, "http://g.cjnep.net/" + url, teacher, speed])
-
- return courses_page_list
-
- def num_input(self, max_num):
- """用户输入"""
- max_num_1 = max_num
- while True:
- try:
- num = int(input("输入数字序号1~{}之间选择开始学习的视频:".format(max_num_1)))
- if num > max_num_1 or num < 1:
- num = int("x")
- break
- except Exception as e:
- print("输入错误,请再输入一遍")
- return num
-
- def course_list(self, courses_page_list):
- """获取单科课程播放列表"""
- num = self.num_input(len(courses_page_list))
- print(courses_page_list[num - 1][2])
- self.driver.get(courses_page_list[num - 1][2])
- # 打开单科课程列表
- my_course_source = self.driver.page_source
- #writer_file.write_file('55.txt', my_course_source)
- # 等待页面跳转,继续学习
- wait = WebDriverWait(self.driver, 1000)
- learn_button = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@class="nm"]')))
- print('准备开始学习')
- continue_learn = re.findall('href="(.*)">(.*)学习', my_course_source)
- print(continue_learn)
- # 打开课程播放界面
- self.driver.get("http://g.cjnep.net" + continue_learn[0][0])
- # continue_learn = re.findall('href="(.*)">继续学习', my_course_source)
- # print(continue_learn)
-
- def play_learn(self):
- """播放界面"""
- wait = WebDriverWait(self.driver, 100000000)
- # 播放按钮
- play_button = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@role="button"]')))
- print('开始播放')
- self.driver.find_elements_by_xpath('//*[@role="button"]')[0].click()
- # 播放结束,下一章按钮
- for x in range(1, 100000):
- next_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="job_nextvideo_btn"]')))
- print('本次已在线学习' + str(x) + '课时,即将进入下一章')
- self.driver.find_element_by_xpath('//*[@id="job_nextvideo_btn"]').click()
-
- def run(self):
- self.login_jiaoyu()
- # time.sleep(20)
- courses_page_list = self.my_course()
- self.course_list(courses_page_list)
- self.play_learn()
-
-
- if __name__ == '__main__':
- shuake = Shuake()
- shuake.run()
复制代码
|
|
|
|
|
|
|
发表于 2021-1-13 15:52:26
举报
回复
|
|