事业工作人员培训平台 Python+selenium
本代码只适用于事业工作人员培训平台
网址https://hnxxpt.zgzjzj.net/
功能:截至文章发出的时候,本程序会按一下顺序检测未完成课程并自动挂机
- 2021年>在岗培训>公共科目
- 2021年>在岗培训>专业科目
- 2022年>在岗培训>公共科目
- 2022年>在岗培训>专业科目
注:本代码并不能提升刷课速度和回答问题/考试等,只是单纯挂机,当检测到视频即将播放完毕时重新播放直到学时完成
提示:我认为用油猴脚本会很简单,但是本人对js和脚本没有过多研究,所以只能采取曲线救国的方式,使用selenium了
API:
登陆网址:'https://hnxxpt.zgzjzj.net/'
课程列表网址:'https://hn.ischinese.cn/learncenter/buycourse'
完整代码
import time
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Lesson:
fail = 0
user, pwd = 0, 0
init_page = 'https://hnxxpt.zgzjzj.net/'
class_page = 'https://hn.ischinese.cn/learncenter/buycourse'
year = 2
kind = 2
def __init__(self, user, pwd):
opt = Options()
# opt.add_argument('--headless')
# opt.add_argument('--disable-gpu')
opt.add_argument('--disable-blink-features=AutomationControlled')
self.browser = webdriver.Chrome(options=opt)
self.user = user
self.pwd = pwd
def init(self):
self.browser.get(self.init_page)
try:
self.browser.find_element(By.XPATH, '/html/body/div[1]/div[1]/div[6]/div/span').click()
self.browser.find_element(By.XPATH, '/html/body/div[1]/div[1]/div[6]/div').click()
except Exception:
print('error')
self.browser.find_element(By.XPATH,
'//*[@id="app"]/div[1]/div[3]/div/div[1]/div[2]/div[2]/div[1]/div[2]/input').send_keys(
self.user)
self.browser.find_element(By.XPATH,
'//*[@id="app"]/div[1]/div[3]/div/div[1]/div[2]/div[2]/div[2]/div/input').send_keys(
self.pwd)
self.browser.find_element(By.XPATH,
'//*[@id="app"]/div[1]/div[3]/div/div[1]/div[2]/div[2]/div[3]/button[1]').click()
sleep(0.1)
# 跳转至课程中心,必须给出时间加载
self.browser.find_element(By.XPATH,
'//*[@id="app"]/div[1]/div[3]/div/div[2]/div[1]/ol/li[1]/img').click()
time.sleep(3)
def get_class(self):
while 1:
try:
self.browser.get(self.class_page)
self.browser.implicitly_wait(1)
# 删除广告栏
self.browser.execute_script("document.getElementsByClassName('leftFixedBox')[0].remove()")
# 选择年份
self.browser.find_element(By.CSS_SELECTOR, f".buyCourse_classType>li:nth-child({self.year})").click()
time.sleep(1)
# 选择课程类型
self.browser.find_element(By.CSS_SELECTOR, f".cur>span:nth-child({self.kind})").click()
time.sleep(1)
# 定位该类第一个课程的进度
locate_cls = (By.CSS_SELECTOR, '.buyCourse_claddProgress>div:last-child')
WebDriverWait(self.browser, 5, 0.5).until(EC.presence_of_element_located(locate_cls))
# 显式等待课程列表加载出来
cls_progress = self.browser.find_element(*locate_cls).text
if cls_progress == '100%':
print('该类课程已经刷完了!')
self.kind -= 1
else:
# 有没完成的课程直接进入
self.browser.find_element(By.XPATH,
'//*[@id="app"]/div[2]/div[3]/div/div[2]/div[4]/div[2]/div/div[1]/div[1]/img').click()
# 调用挂机函数
self.check()
if self.kind == 0:
print('该年度的课程已经刷完!')
self.year -= 1
self.kind = 2
if self.year == 0:
print('全部年度的课程已经刷完!程序结束!')
return
if self.fail >= 20:
exit(-1)
except Exception as e:
self.fail += 1
self.browser.refresh()
print(repr(e))
time.sleep(10)
def check(self):
# 隐式等待
self.browser.implicitly_wait(10)
# 下面是专门给视频的加载时间
time.sleep(3)
# 自动静音
self.browser.execute_script("document.getElementsByClassName('vjs-tech')[0].muted=true")
duration = self.browser.execute_script("return document.getElementsByClassName('vjs-tech')[0].duration")
while 1:
try:
current_time = self.browser.execute_script(
"return document.getElementsByClassName('vjs-tech')[0].currentTime")
progress = self.browser.find_element(By.CSS_SELECTOR, '.text>span:last-child').text
print('now:', current_time, 'whole:', duration)
print(progress)
# 如果视频暂停自动播放
video_statu = self.browser.execute_script(
"return document.getElementsByClassName('vjs-tech')[0].paused")
if video_statu:
self.browser.execute_script("return document.getElementsByClassName('vjs-tech')[0].play()")
if int(duration) - int(current_time) <= 210:
print('检测到视频快结束,已经自动重置视频进度')
self.browser.execute_script("document.getElementsByClassName('vjs-tech')[0].currentTime=0")
if progress == '100%':
print('检测到课程完成,寻找下一个课程')
break
# 每隔100s进行一次检查
sleep(100)
except Exception as e:
print(repr(e))
if __name__ == '__main__':
web = Lesson('username', 'password')
web.init()
web.get_class()
本代码仅供学术研究,作者不承担任何后果
本文来自博客园,作者:Hello418,转载请注明原文链接:https://www.cnblogs.com/janitor/p/16390845.html