#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 播放视频并截屏,查看字幕 """ import asyncio from playwright.async_api import async_playwright async def play_and_screenshot(bvid): """播放视频并截屏""" url = f"https://www.bilibili.com/video/{bvid}" async with async_playwright() as playwright: browser = await playwright.chromium.launch(headless=False) # 使用有头模式 context = await browser.new_context(viewport={'width': 1920, 'height': 1080}) page = await context.new_page() print(f"正在访问视频: {url}") print("=" * 80) print("\n1. 访问页面...") await page.goto(url, wait_until='networkidle', timeout=60000) await asyncio.sleep(3) print("2. 截取初始状态...") await page.screenshot(path="/tmp/bilibili_1_initial.png") print("3. 尝试点击播放按钮...") try: # 尝试多个可能的播放按钮选择器 selectors = [ '.bpx-player-ctrl-play', '.bpx-player-shadow-play-area', 'video', ] for selector in selectors: element = await page.query_selector(selector) if element: await element.click() print(f" ✅ 已点击 {selector}") break except Exception as e: print(f" ⚠️ 点击播放失败: {e}") print("4. 等待视频播放...") await asyncio.sleep(10) print("5. 截取播放中状态...") await page.screenshot(path="/tmp/bilibili_2_playing.png") print("6. 尝试点击字幕按钮...") try: # 查找字幕按钮 subtitle_btn = await page.query_selector('.bpx-player-ctrl-subtitle') if subtitle_btn: is_enabled = await subtitle_btn.is_enabled() print(f" 字幕按钮状态: {'可用' if is_enabled else '不可用'}") if is_enabled: await subtitle_btn.click() print(" ✅ 已点击字幕按钮") await asyncio.sleep(2) await page.screenshot(path="/tmp/bilibili_3_subtitle_on.png") except Exception as e: print(f" ⚠️ 点击字幕按钮失败: {e}") print("7. 获取页面文本内容(包含字幕)...") try: # 获取所有文本 body_text = await page.evaluate('() => document.body.innerText') # 查找可能包含字幕的元素 subtitle_elements = await page.query_selector_all('[class*="subtitle"], [class*="caption"]') print(f" 找到 {len(subtitle_elements)} 个可能的字幕元素") for i, elem in enumerate(subtitle_elements[:5]): text = await elem.inner_text() if text.strip(): print(f" [{i+1}] {text[:100]}") except Exception as e: print(f" ⚠️ 获取文本失败: {e}") print("\n8. 检查页面中是否有动态字幕数据...") try: script_result = await page.evaluate(''' () => { const results = { hasSubtitlePlugin: false, subtitleText: '', captions: [], }; // 检查播放器实例 const playerContainer = document.querySelector('.bpx-player-container'); if (playerContainer) { results.playerExists = true; } // 查找字幕元素 const captionElements = document.querySelectorAll('.bpx-player-caption'); captionElements.forEach(el => { results.captions.push(el.innerText); }); return results; } ''') print(f" 检查结果: {script_result}") except Exception as e: print(f" ⚠️ 检查失败: {e}") print("\n9. 等待30秒查看效果...") await asyncio.sleep(30) print("10. 截取最终状态...") await page.screenshot(path="/tmp/bilibili_4_final.png") print("\n" + "=" * 80) print("测试完成!请检查以下截图:") print(" /tmp/bilibili_1_initial.png - 初始状态") print(" /tmp/bilibili_2_playing.png - 播放中") print(" /tmp/bilibili_3_subtitle_on.png - 开启字幕后(如果有)") print(" /tmp/bilibili_4_final.png - 最终状态") print("=" * 80) print("\n按回车键关闭浏览器...") # 等待用户确认(但在自动化环境中会自动继续) await asyncio.sleep(5) await browser.close() if __name__ == '__main__': bvid = "BV1KtFzzqEUG" asyncio.run(play_and_screenshot(bvid))