jimlv commited on
Commit
e058bc8
·
verified ·
1 Parent(s): 3d852be

Create utils/browser_automation.py

Browse files
Files changed (1) hide show
  1. utils/browser_automation.py +21 -0
utils/browser_automation.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import websockets
3
+ import json
4
+
5
+ LIGHTPANDA_WS_URL = "ws://localhost:9222"
6
+
7
+ async def fetch_page(url: str):
8
+ # 示例:通过 WebSocket 连接 Lightpanda 的 CDP 服务
9
+ try:
10
+ async with websockets.connect(LIGHTPANDA_WS_URL) as websocket:
11
+ # 发送 CDP 命令(以 Page.navigate 为例)
12
+ command = {
13
+ "id": 1,
14
+ "method": "Page.navigate",
15
+ "params": {"url": url}
16
+ }
17
+ await websocket.send(json.dumps(command))
18
+ response = await websocket.recv()
19
+ return f"<pre>成功导航至 {url}\n响应: {response}</pre>"
20
+ except Exception as e:
21
+ return f"<pre>与 Lightpanda 连接出错: {e}</pre>"