from DrissionPage import ChromiumPage
import time
def play_bilibili_video(url):
# 初始化 ChromiumPage,设置无头模式为 False 以便观察浏览器行为
page = ChromiumPage(headless=False)
try:
# 打开 B 站视频页面
page.get(url, timeout=30)
# 等待视频元素加载完成
page.wait.until('video', timeout=30)
# 找到视频元素并播放
video = page.ele('video')
video.click() # 点击视频以开始播放(有时需要点击才能播放)
# 等待视频播放完毕
while True:
# 检查视频是否仍在播放
state = video.evaluate('element => element.paused')
if state:
break
time.sleep(1)
print("当前视频播放完毕")
except Exception as e:
print(f"播放过程中出现错误: {e}")
finally:
# 关闭浏览器
page.close()
def play_videos_sequentially(video_urls):
for url in video_urls:
print(f"正在播放: {url}")
play_bilibili_video(url)
time.sleep(2) # 等待几秒后播放下一个视频
if __name__ == "__main__":
# 示例视频链接列表
video_urls = [
"https://www.bilibili.com/video/BV1xxxxxx", # 替换为实际的 B 站视频链接
"https://www.bilibili.com/video/BV2xxxxxx",
# 添加更多视频链接
]
play_videos_sequentially(video_urls)