暂时只有刷视频的功能。步骤如下:
1.先下一个谷歌浏览器,安装完成后右键点击打开属性,在目标后空一格加上--remote-debugging-port=9222 --user-data-dir="C:\selenium\ChromeProfile"
后点击确定
因为登录需要扫码,所以先扫码登录之后再进行刷课。此步骤是为了selenium可以控制当前打开的页面,而不是重新打开新的页面,这样子就可以省略掉扫码登录的步骤。
2.接着打开浏览器登录官网到达课程页面。(确保此时只有一个打开的页面,刷课期间不要再使用谷歌浏览器)
3.下载脚本,直接点击下载就行不用积分。下载解压后直接运行dist下的muke.exe即可。
4.关于关闭以及下次打开
直接关闭即可,下次使用只需要按照步骤2打开一个页面再次运行exe文件即可。
5.代码
其实只是使用selenium进行简单的操作,不用手动挂课还是方便一些。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time
import sys
from selenium.webdriver import ActionChains
def print_progress_bar(current_time, total_time):
current_seconds = sum(int(x) * 60 ** i for i, x in enumerate(reversed(current_time.split(':'))))
total_seconds = sum(int(x) * 60 ** i for i, x in enumerate(reversed(total_time.split(':'))))
progress = current_seconds / total_seconds
bar_length = 40 # 进度条长度
filled_length = int(bar_length * progress)
bar = '█' * filled_length + '-' * (bar_length - filled_length)
sys.stdout.write(f'\r进度: |{bar}| {current_time}/{total_time}')
sys.stdout.flush()
def shuake():
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(options=chrome_options)
# 直接获取所有视频图标的父元素
video_elements = driver.find_elements(By.XPATH, "//i[contains(@class, 'icon--shipin')]/..")
count = len(video_elements)
before = driver.current_window_handle
print("一共有{}个视频。".format(count))
for i in range(0, count):
print("现在看到第{}个视频".format(i + 1))
driver.execute_script("arguments[0].click();", video_elements[i])
# 获取所有窗口句柄
windows = driver.window_handles
#切换到新打开的页面
driver.switch_to.window(windows[1])
# 等待视频加载
time.sleep(8)
# 检查播放进度
percent = driver.find_element(By.CSS_SELECTOR, '#video-box > div > xt-wrap > xt-controls > xt-inner > xt-time')
text = percent.text
current_time, total_time = text.split(' / ')
print_progress_bar(current_time, total_time)
# 判断是否已播放完毕
if current_time == total_time:
print("播放完毕,进行下一个视频")
driver.close()
driver.switch_to.window(driver.window_handles[0])
time.sleep(2)
continue # 跳过播放,直接进入下一个视频
# 没播放完毕开始播放视频
yinliang_bottun = driver.find_element(By.XPATH,'//*[@id="video-box"]/div/xt-wrap/xt-controls/xt-inner/xt-volumebutton/xt-icon')
yinliang_bottun.click()
video_play_button = driver.find_element(By.XPATH, '//*[@id="video-box"]/div/xt-wrap/xt-controls/xt-inner/xt-playbutton')
video_play_button.click()
# 获取视频元素
video_element = driver.find_element(By.XPATH, '//*[@id="video-box"]')
action = ActionChains(driver)
# 定期检查
while True:
# 模拟鼠标悬停保持进度条可见
action.move_to_element(video_element).perform()
percent = driver.find_element(By.CSS_SELECTOR, '#video-box > div > xt-wrap > xt-controls > xt-inner > xt-time')
text = percent.text
current_time, total_time = text.split(' / ')
print_progress_bar(current_time, total_time)
# 获取元素的类属性判断是否正在播放
element = driver.find_element(By.CSS_SELECTOR, "xt-bigbutton.xt_video_player_big_play_layer")
class_attribute = element.get_attribute("class")
#防止点击到暂停
if 'pause' in class_attribute and total_time != current_time:
video_play_button.click()
if current_time == total_time:
print("\n播放完毕,进行下一个视频")
driver.close()
driver.switch_to.window(driver.window_handles[0])
break # 结束当前循环,开始下一个视频
time.sleep(5) # 每5秒检测一次
if __name__ == "__main__":
shuake()