代码拉取完成,页面将自动刷新
# encoding:gbk
import sys
import time
# 导入 webdriver
from selenium import webdriver
# 要想调用键盘按键操作需要引入keys包
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
import random
# 设置浏览器选项
edge_options = Options()
edge_options.use_chromium = True
edge_options.add_experimental_option("excludeSwitches",['enable-automation','enable-logging']) #禁止打印日志,规避检测
edge_options.add_argument('--mute-audio')#浏览器静音
edge_options.add_argument("--incognito") # 配置隐私模式
edge_options.add_argument('--headless')# 设置无界面模式
edge_options.add_argument('--disable-gpu') #设置不弹出界面
edge_options.add_argument('disable-infobars')
edge_options.add_argument('log-level=3')
# 调用环境变量指定的创建浏览器对象
s = Service(executable_path='./msedgedriver')
driver = webdriver.Edge(service=s, options=edge_options)
driver.implicitly_wait(time_to_wait=4)
driver.maximize_window() # 最大化窗口
# 联通网上学院
driver.get("http://wsxy.chinaunicom.cn/learner/home")
def login(username,password):
print(f'正在登录--用户:{username}')
from tools.ocr_yzm import img2str
while 1:
driver.find_element(by=By.XPATH,
value='/html/body/spk-root/spk-login-page/div/section/div[3]/div[2]/div/nz-tabset/div/div/nz-tab-body[1]/form/div[3]/div[2]/img').click()
time.sleep(1)
yzm = driver.find_element(by=By.XPATH,
value='/html/body/spk-root/spk-login-page/div/section/div[3]/div[2]/div/nz-tabset/div/div/nz-tab-body[1]/form/div[3]/div[2]/img')
yzm.screenshot('yzm.png')
res = img2str()
time.sleep(1)
if len(res) != 3 or len(res['words_result'])<1:
continue
yzmStr = res['words_result'][0]['words'].replace(' ','')
if len(yzmStr) != 4:
continue
driver.find_element(by=By.XPATH,value='/html/body/spk-root/spk-login-page/div/section/div[3]/div[2]/div/nz-tabset/div/div/nz-tab-body[1]/form/div[1]/div/div/nz-input/input').clear()
driver.find_element(by=By.XPATH,value='/html/body/spk-root/spk-login-page/div/section/div[3]/div[2]/div/nz-tabset/div/div/nz-tab-body[1]/form/div[1]/div/div/nz-input/input').send_keys(username)
driver.find_element(by=By.XPATH,value='/html/body/spk-root/spk-login-page/div/section/div[3]/div[2]/div/nz-tabset/div/div/nz-tab-body[1]/form/div[2]/div/div/nz-input/input').clear()
driver.find_element(by=By.XPATH,value='/html/body/spk-root/spk-login-page/div/section/div[3]/div[2]/div/nz-tabset/div/div/nz-tab-body[1]/form/div[2]/div/div/nz-input/input').send_keys(password)
driver.find_element(by=By.XPATH,value='/html/body/spk-root/spk-login-page/div/section/div[3]/div[2]/div/nz-tabset/div/div/nz-tab-body[1]/form/div[3]/div[2]/input').clear()
driver.find_element(by=By.XPATH,value='/html/body/spk-root/spk-login-page/div/section/div[3]/div[2]/div/nz-tabset/div/div/nz-tab-body[1]/form/div[3]/div[2]/input').send_keys(yzmStr)
driver.find_element(by=By.XPATH,value='/html/body/spk-root/spk-login-page/div/section/div[3]/div[2]/div/nz-tabset/div/div/nz-tab-body[1]/form/div[4]/div/div/button').click()#登录
time.sleep(3)
url = driver.current_url
if url.endswith('login'):#登录失败
print('验证码识别错误,重新识别中...')
else:
print('登录成功!进入专区挂课...')
break
def choseCourse(type):
time.sleep(1)
if type == '未学习':
driver.find_element(by=By.XPATH,
value='/html/body/spk-root/spk-home/div/nz-spin/div[2]/spk-special-area-detail/div/div[3]/spk-area-all-course/div/div[2]/div[2]/span[2]').click()
elif type == '未完成':
driver.find_element(by=By.XPATH,
value='/html/body/spk-root/spk-home/div/nz-spin/div[2]/spk-special-area-detail/div/div[3]/spk-area-all-course/div/div[2]/div[2]/span[4]').click()
time.sleep(1)
courses = driver.find_elements(by=By.CLASS_NAME,value='single-course')
if len(courses)==0 and type =='未完成':
print('专区所有课程已挂完,程序退出...')
return 0
elif len(courses)==0:
choseCourse('未完成')
else:
courses[random.randint(0, len(courses)-1)].click()
driver.switch_to.window(driver.window_handles[-1])#切换窗口句柄
driver.find_elements(by=By.CLASS_NAME,value='course-button')[0].click()#进入播放页面
videoIframe = driver.find_element(by=By.TAG_NAME,value='iframe')
driver.switch_to.frame(videoIframe)#切换句柄到视频iframe
video = driver.find_element(by=By.TAG_NAME,value='video')
driver.execute_script(f'arguments[0].volume=0;', video) # 设置视频静音
print()
while 1:
curTime = float(video.get_attribute('currentTime'))
allTime = float(video.get_attribute('duration'))
print('\r当前播放:《'+driver.title + '》\t进度-->%.3f分/%.3f分' %((curTime/60) ,(allTime/60)),end="",flush=True)
time.sleep(8)
driver.switch_to.window(driver.window_handles[-1]) # 切换回窗口句柄
driver.find_element(by=By.XPATH,value='/html/body/spk-root/spk-player/div/div[1]/div[4]/button').click()#保存进度
driver.switch_to.frame(videoIframe) # 切换句柄到视频iframe
if curTime == allTime or curTime == float(video.get_attribute('currentTime')): #播放完或者时间未动
driver.switch_to.window(driver.window_handles[-1]) # 切换回窗口句柄
driver.find_element(by=By.XPATH,
value='/html/body/spk-root/spk-player/div/div[1]/div[1]').click()#返回
time.sleep(2)
driver.get(shuakeUrl)
choseCourse('未学习')
break
import atexit
@atexit.register
def f():
driver.quit() #关闭浏览器
print('结束')
time.sleep(1)
if __name__ == '__main__':
with open('./userpass.ini', mode='r+',encoding='utf-8') as f:
user = f.readline().split('=')[1].strip()
passwd = f.readline().split('=')[1].strip()
shuakeUrl = f.readline().split('=')[1].strip()
type = f.readline().split('=')[1].strip()
login(user,passwd)
driver.get(shuakeUrl) #进入专区课程页面
time.sleep(2)
print(f'挂课的专区:\t{driver.title}')
keshi = driver.find_elements(by=By.CLASS_NAME,value="info-peroid")[0].text.replace("\n","").replace(" ","")
print(f'专区时长:\t{keshi}')
choseCourse(type)#选择课程开始挂课
#选择未学习课程
# driver.find_element(by=By.XPATH,value='/html/body/spk-root/spk-home/div/nz-spin/div[2]/spk-special-area-detail/div/div[3]/spk-area-all-course/div/div[2]/div[2]/span[4]').click()
# courses = driver.find_elements(by=By.CLASS_NAME,value='single-course')
# courses[0].click()
# driver.switch_to.window(driver.window_handles[-1])#切换窗口句柄
# driver.find_elements(by=By.CLASS_NAME,value='course-button')[0].click()
#
# videoIframe = driver.find_element(by=By.TAG_NAME,value='iframe')
# driver.switch_to.frame(videoIframe)#切换句柄到视频iframe
# video = driver.find_element(by=By.TAG_NAME,value='video')
# curTime = float(video.get_attribute('currentTime'))
# allTime = float(video.get_attribute('duration'))
# driver.execute_script(f'arguments[0].volume=0;', video) # 设置视频静音
#
# driver.switch_to.window(driver.window_handles[-1])#切换回窗口句柄
# driver.find_element(by=By.XPATH,value='/html/body/spk-root/spk-player/div/div[1]/div[4]/button/span').click()
# items = driver.find_elements(by=By.CLASS_NAME,value='course-single')
# items[0].find_element(by=By.CLASS_NAME,value='title').text
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。