-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_live.py
More file actions
82 lines (69 loc) · 2.77 KB
/
Copy pathdebug_live.py
File metadata and controls
82 lines (69 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- encoding: utf-8 -*-
import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
import asyncio
from playwright.async_api import async_playwright
import time
import json
async def main():
danmaku_ws_url = None
captured_messages = []
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
context = await browser.new_context(
storage_state=None # 不使用存储状态,使用当前浏览器cookie
)
page = await context.new_page()
# 拦截WebSocket
async def on_websocket(ws):
print(f"[WebSocket连接] {ws.url[:200]}")
nonlocal danmaku_ws_url
if 'webcast' in ws.url or 'chat' in ws.url or 'im' in ws.url:
danmaku_ws_url = ws.url
try:
async for msg in ws.incoming_messages():
msg_text = str(msg)[:200]
print(f"[WS消息] {msg_text}")
captured_messages.append(msg_text)
except Exception as e:
print(f"[WS错误] {e}")
page.on('websocket', on_websocket)
print("访问直播间...")
await page.goto('https://live.douyin.com/YOUR_ROOM_ID', timeout=60000) # replace with your live room ID
print("\n等待页面加载(30秒)...")
await page.wait_for_timeout(30000)
print("\n截图...")
await page.screenshot(path='./test_danmaku.png')
print(f"\n弹幕WebSocket URL: {danmaku_ws_url}")
print(f"捕获消息数: {len(captured_messages)}")
for i, msg in enumerate(captured_messages[:10]):
print(f" [{i}] {msg}")
# 尝试执行JS查看页面状态
print("\n尝试获取Vue/React状态...")
try:
state = await page.evaluate("""
JSON.stringify(window.__INITIAL_STATE__ || {})
""")
if state and state != '{}':
print("INITIAL_STATE:", state[:500])
except:
pass
# 查找弹幕相关元素
print("\n搜索页面中的弹幕内容...")
try:
# 搜索页面中所有包含用户名的元素
all_text = await page.evaluate("""
Array.from(document.querySelectorAll('span, div, p'))
.map(el => el.innerText)
.filter(t => t.length > 1 && t.length < 100)
.filter(t => !t.includes('\\n'))
.slice(0, 50)
""")
print("找到的文本片段:")
for t in all_text[:20]:
print(f" {t[:80]}")
except Exception as e:
print(f"错误: {e}")
await browser.close()
print("\n完成")
asyncio.run(main())