基于Python和selenium的广东省专业技术人员远程教育学习脚本
本帖最后由 lntuer 于 2019-10-18 11:54 编辑一、介绍
广东省专业技术人员远程教育网址:http://ggfw.gdhrss.gov.cn/ssologin/login?service=http://ggfw.gdhrss.gov.cn/gdggfw/index.shtml
二、功能
①基于Python和selenium半自动化登陆、pyautogui模块;
②自动学习该课程目录下所有课程和学习;
③可以后台观看视频并静音播放
代码如下:
from selenium import webdriver
import pyautogui
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import re
import random
username="你的账号:"
passwd="你的密码:"
login_url='http://ggfw.gdhrss.gov.cn/ssologin/login?service=http://ggfw.gdhrss.gov.cn/gdggfw/index.shtml'
option = webdriver.ChromeOptions()
option.add_argument('--mute-audio')
browser = webdriver.Chrome(chrome_options=option)
browser.maximize_window()
browser.get(login_url)
browser.implicitly_wait(10)
elem=browser.find_element_by_id("username_personal")
elem.clear()
elem.send_keys(username)
time.sleep(1)
elem=browser.find_element_by_id("password_personal")
elem.clear()
elem.send_keys(passwd)
time.sleep(1)
code_num=pyautogui.prompt("请输入验证码:")
elem=browser.find_element_by_id("vcode_personal")
elem.clear()
elem.send_keys(code_num)
elem=browser.find_element_by_id('doPersonLogin')
elem.click()
time.sleep(3)
browser.get('需要学习的课程目录地址:')
time.sleep(3)
js_001='document.getElementsByClassName("append-plugin-tip").click();'
js_005='return document.getElementsByClassName("append-plugin-tip").length;'
browser.execute_script(js_001)
time.sleep(3)
video_unstudy_num=browser.execute_script(js_005)
time.sleep(3)
for i in range(0,int(video_unstudy_num)):
js_006='document.getElementsByClassName("append-plugin-tip")[{}].click();'.format(i)
browser.execute_script(js_006)
time.sleep(1)
js_002 = 'p.play()'
browser.execute_script(js_002)
time.sleep(1)
js_003 = 'return document.getElementById("realPlayVideoTime").textContent;'
js_004 = 'return document.getElementById("askTime").textContent;'
video_real_time = browser.execute_script(js_003)
time.sleep(1)
video_total_time = browser.execute_script(js_004)
print(video_real_time,video_total_time)
remain_time = int((int(str(video_total_time)[:2]) * 3600 + int(str(video_total_time)) * 60 + int(str(video_total_time)[-2:]))*((101-int(video_real_time))/100))
print("该课程还有{}秒未学习……".format(remain_time))
first_time=time.time()
print(first_time)
def find_question(count_time):
try:
WebDriverWait(browser,count_time,0.5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="aadiv"]/div')))
browser.find_element_by_xpath('//*[@id="aadiv"]/div').click()
time.sleep(1)
browser.find_element_by_xpath('//*[@id="from_ejectque"]/div/a').click()
time.sleep(1)
browser.find_element_by_xpath("/html/body/div/div/a/span/span").click()
print("答题完毕……")
except:
print("未发现需要答题的题目……")
find_question(remain_time)
second_time=time.time()
print(second_time)
first_remain_time=int(second_time - first_time)
print("第一次答题共等待了{}秒".format(first_remain_time))
third_time=time.time()
print(third_time)
time.sleep(3)
find_question(remain_time-first_remain_time+10)
time.sleep(3)
four_time=time.time()
print(four_time)
second_remain_time=int(four_time-third_time)
print("第二次答题共等待了{}秒".format(second_remain_time))
for unstart_time in range(0,remain_time-first_remain_time+10-second_remain_time):
print("该课程还有{}秒未学习……".format(remain_time-first_remain_time+10-second_remain_time-unstart_time))
time.sleep(1)
time.sleep(3)
有的老铁反映Python和selenium安装比较麻烦(但是自动化程度高啊),下面还有JavaScript代码,写的比较水,可以用但是不能全部自动化,具体用法为F12,打开开发者工具输入代码回车:
setInterval(function(){
document.getElementsByClassName("exam-subject-text-queanswar-answer").click();
document.getElementsByClassName("reply-sub").click();
document.getElementsByClassName("l-btn-text").click();
},1000)
声明:仅限个人学习使用,严禁商业使用,否则后果自负,如有违规,管理直接删帖!
最后,原创不易,还希望大家可以高抬贵手免费评个分!!! v.n.lee 发表于 2019-10-19 16:06
大佬厉害,有js操作的教程吗想学习学习
我也是网上找的,JavaScript基础很差,之前自学Python q6378561 发表于 2019-10-18 13:43
表面python 本质js
很中肯,但是我现在在学习JavaScript,想二者结合一下! 大佬牛皮! 感谢分享,怎么扩展到其他省的平台呢 感谢分享 表面python 本质js 感谢分享,这是非常实用的东西! 基于Python下的JS吗? 大佬厉害,有js操作的教程吗想学习学习
页:
[1]
2