好友
阅读权限20
听众
最后登录1970-1-1
|
本帖最后由 MyModHeaven 于 2022-6-25 19:02 编辑
2022.6.25
[Python] 纯文本查看 复制代码
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.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
class Browser:
login_token = {}
phone_number = ''
course1 = ''
course2 = ''
login_url = 'https://www.mosoteach.cn/web/index.php?c=passport&m=index'
def __init__(self):
options = Options()
options.page_load_strategy = 'eager'
self.browser = webdriver.Chrome(options=options)
self.browser.maximize_window()
# 修改 window.navigator.webdriver 的值
self.browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
'source':'''
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})'''
})
def get_cookie(self):
self.browser.get(self.login_url)
self.browser.find_element(By.ID, 'phone-login-button').click() # 切换到短信验证码登录
self.browser.find_element(By.ID, 'phone-login-remember-me').click() # 勾选 30 天自动登录
self.browser.find_element(By.ID, 'phone-number').send_keys(self.phone_number)
slider = self.browser.find_element(By.XPATH, '//div[@class="slider-box"]//span[@class="nc_iconfont btn_slide"]')
size = self.browser.find_element(By.XPATH, '//div[@class="slider-box"]//div[@class="scale_text slidetounlock"]').size
ActionChains(self.browser).drag_and_drop_by_offset(slider, size['width'], -size['height']).perform() # 滑动滑块
button = self.browser.find_element(By.ID, 'get-verify-code')
WebDriverWait(self.browser, 3).until(EC.element_to_be_clickable(button)) # 判断节点是否可以点击
button.click()
input = self.browser.find_element(By.XPATH, '//div[@class="verify-box"]/input')
input.click() # 将光标定位到输入框
# 判断验证码是否输入完毕
while True:
verify_code = input.get_attribute('value')
if len(verify_code) == 4:
break
self.browser.find_element(By.ID, 'login-button-2').click()
WebDriverWait(self.browser, 30).until(EC.title_is('云班课 - 我的班课'))
cookie = self.browser.get_cookie('login_token')
with open('d:/cookies.txt', 'w', encoding='utf-8') as f:
f.write(str(cookie))
# 勾选 30 天自动登录后,才有 login_token 这个 cookie
def login(self):
self.browser.get(self.login_url)
self.browser.delete_all_cookies()
self.browser.add_cookie(self.login_token)
self.browser.get(self.login_url)
def browse(self):
self.browser.get(self.course2) # 打开课程
hide_div = self.browser.find_elements(By.CLASS_NAME, 'hide-div')
for div in hide_div:
print(div.find_element(By.XPATH, './preceding-sibling::*').find_element(By.TAG_NAME, 'span').text)
self.browser.execute_script('arguments[0].scrollIntoView();', div) # 将页面跳转到元素的位置
remain = div.find_element(By.XPATH, './preceding-sibling::*/span[3]').text
if remain == '0': # 判断是否有未读文件
continue
if div.get_attribute('data-status') == 'N': # 判断是否已展开
div.find_element(By.XPATH, './preceding-sibling::*').click() # 展开分组
resource = div.find_elements(By.CLASS_NAME, 'res-info')
# 资源有:.jpg(Esc 键关闭)、.pdf/.pptx/.doc/.xls/网页链接(新标签页打开)、视频(手动播放)
for res in resource:
self.browser.execute_script('arguments[0].scrollIntoView();', res) # 将页面跳转到元素的位置
if res.find_elements(By.XPATH, './div[2]/span')[-3].get_attribute('data-is-drag') == 'N': # 筛选出未读的资源
filename = res.find_element(By.CLASS_NAME, 'res-name').text
print(filename)
if filename.endswith('.mp4'): # 跳过视频
continue
elif filename.endswith('.jpg'): # 图片
print('正在打开图片:', filename)
res.click()
sleep(2) # 视网络情况可适当修改时间,建议2~3秒
# self.browser.find_element(By.XPATH, '//div[@class="viewer-canvas"]/img').send_keys(Keys.ESCAPE)
# 👆,想通过 Esc 键关闭图片,但是一直报错,怀疑没有找对施加 Esc 键的元素
self.browser.find_element(By.XPATH, '//div[@class="viewer-button viewer-close"]').click()
# 在新标签页中打开,最后一起关闭。但是当标签页太多,可能会出现问题
else: # 文件或者网页,在新标签页打开
print('正在打开资源:', filename)
ActionChains(self.browser).key_down(Keys.CONTROL).perform()
res.click()
ActionChains(self.browser).key_up(Keys.CONTROL).perform()
Browser = Browser()
#Browser.get_cookie()
Browser.login()
Browser.browse()
- 现在云班课的经验值获取规则是这样的:视频看完才有,图片/ppt/doc/pdf/xlsx/网页链接点开就有
- 视频手动刷,其他的这个程序可以刷
- 账号、cookie、课程网址需要填到代码中
[Python] 纯文本查看 复制代码
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.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
class Browser:
def __init__(self):
options = Options()
options.page_load_strategy = 'eager'
self.browser = webdriver.Chrome(options=options)
self.browser.maximize_window()
# 修改 window.navigator.webdriver 的值,不然滑块验证过不去
self.browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {
'source':'''
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})'''
})
def get_cookies(self):
self.browser.get('https://www.mosoteach.cn/web/index.php?c=passport&m=index')
self.browser.find_element(By.ID, 'phone-login-button').click() # 切换到短信验证码登录
self.browser.find_element(By.ID, 'phone-login-remember-me').click() # 勾选 30 天自动登录
self.browser.find_element(By.ID, 'phone-number').send_keys('') # 输入手机号
slider = self.browser.find_element(By.XPATH, '//div[@class="slider-box"]//span[@class="nc_iconfont btn_slide"]')
size = self.browser.find_element(By.XPATH, '//div[@class="slider-box"]//div[@class="scale_text slidetounlock"]').size
ActionChains(self.browser).drag_and_drop_by_offset(slider, size['width'], -size['height']).perform() # 滑动滑块
button = self.browser.find_element(By.ID, 'get-verify-code')
WebDriverWait(self.browser, 3).until(EC.element_to_be_clickable(button)) # 判断节点是否可以点击
button.click()
input = self.browser.find_element(By.XPATH, '//div[@class="verify-box"]/input')
input.click() # 将光标定位到输入框
# 判断验证码是否输入完毕
while True:
verify_code = input.get_attribute('value')
if len(verify_code) == 4:
break
self.browser.find_element(By.ID, 'login-button-2').click()
WebDriverWait(self.browser, 30).until(EC.title_is('云班课 - 我的班课'))
cookies = self.browser.get_cookie('login_token')
with open('d:/cookies.txt', 'w', encoding='utf-8') as f:
f.write(str(cookies))
# 勾选 30 天自动登录后,才有 login_token 这个 cookie
def login(self):
self.browser.get('https://www.mosoteach.cn/web/index.php?c=passport&m=index')
self.browser.delete_all_cookies()
self.browser.add_cookie(cookie) # 输入你的 cookie
self.browser.get('https://www.mosoteach.cn/web/index.php?c=passport&m=index')
def browse(self):
self.browser.get(' ') # 课程网址
hide_div = self.browser.find_elements(By.CLASS_NAME, 'hide-div')
for div in hide_div:
self.browser.execute_script('arguments[0].scrollIntoView();', div) # 将页面跳转到元素的位置
remain = div.find_element(By.XPATH, './preceding-sibling::*/span[3]').text
if remain == '0' or div.get_attribute('data-status') == 'Y': # 判断是否有未读文件,有的话再判断分组是否展开
continue
div.find_element(By.XPATH, './preceding-sibling::*').click() # 展开分组
resource = div.find_elements(By.CLASS_NAME, 'res-info')
# 资源有:.jpg(Esc 键关闭)、.pdf/.pptx/.doc/.xls/网页链接(新标签页打开)、视频(手动播放)
for res in resource:
self.browser.execute_script('arguments[0].scrollIntoView();', res) # 将页面跳转到元素的位置
if res.find_element(By.XPATH, './div[2]/span[7]').get_attribute('data-is-drag') == 'N': # 筛选出未读的资源
filename = res.find_element(By.CLASS_NAME, 'res-name').text
type = res.find_element(By.XPATH, './parent::*').get_attribute('class').split()[2] # 判断是否是网页
judge = {filename.endswith(i) for i in ['.pdf', '.pptx', '.doc', '.xls']} # 判断是否是文件
if filename.endswith('.jpg'): # 图片
print('正在打开图片:', filename)
res.click()
sleep(3)
# self.browser.find_element(By.XPATH, '//div[@class="viewer-canvas"]/img').send_keys(Keys.ESCAPE)
# 👆,想通过 Esc 键关闭图片,但是一直报错,怀疑没有找对施加 Esc 键的元素
self.browser.find_element(By.XPATH, '//div[@class="viewer-button viewer-close"]').click()
# 在新标签页中打开,最后一起关闭。但是当标签页太多,可能会出现问题
elif judge == {True, False} or type == 'web': # 需要新标签页打开的
print('正在打开资源:', filename)
ActionChains(self.browser).key_down(Keys.CONTROL).perform()
res.click()
ActionChains(self.browser).key_up(Keys.CONTROL).perform()
Browser = Browser()
Browser.login()
Browser.browse()
|
|