2 Commits

Author SHA1 Message Date
f7f14d7114 恢复端口为16025 2026-07-07 13:23:36 +08:00
ef4d26f11b 修改默认端口为16026 2026-07-07 13:20:52 +08:00
2 changed files with 129 additions and 2 deletions

62
app.py
View File

@@ -364,6 +364,59 @@ async def capture_with_playwright(
return {"success": False, "error": str(e)}
async def capture_with_cdp(
url_hint: str = "",
action: str = "screenshot",
cdp_port: int = 9222
):
"""
使用 CDP 连接到已打开的 Chrome 浏览器
用于方案三:手动验证后自动截图
"""
if not PLAYWRIGHT_AVAILABLE:
return {"success": False, "error": "Playwright not installed"}
try:
async with async_playwright() as p:
# 连接到已运行的 Chrome
browser = await p.chromium.connect_over_cdp(f"http://localhost:{cdp_port}")
# 获取所有页面
contexts = browser.contexts
result_data = []
for context in contexts:
for page in context.pages:
current_url = page.url
title = await page.title()
# 如果 URL 包含提示词或者是唯一页面
if url_hint in current_url or not url_hint:
if action == "screenshot":
temp_file = CAPTURE_DIR / f"cdp_capture_{uuid.uuid4()[:8]}.png"
await page.screenshot(path=str(temp_file), full_page=True)
result_data.append({
"url": current_url,
"title": title,
"file_path": str(temp_file)
})
elif action == "html":
html = await page.content()
result_data.append({
"url": current_url,
"title": title,
"html": html
})
if result_data:
return {"success": True, "pages": result_data}
else:
return {"success": False, "error": "No matching page found"}
except Exception as e:
return {"success": False, "error": f"CDP connection failed: {str(e)}"}
def capture_webpage(
url: str,
action: str = "screenshot",
@@ -467,7 +520,8 @@ def capture():
"full_page": false,
"viewport": {"width": 1920, "height": 1080},
"wait_time": 2000,
"backend": "auto" | "agent-browser" | "playwright"
"backend": "auto" | "agent-browser" | "playwright" | "chrome-cdp",
"cdp_port": 9222 // 用于 chrome-cdp 后端,连接到已打开的 Chrome
}
"""
data = request.get_json()
@@ -490,6 +544,8 @@ def capture():
viewport = data.get("viewport")
wait_time = int(data.get("wait_time", 2000))
backend = data.get("backend", "auto")
cdp_port = int(data.get("cdp_port", 9222))
url_hint = data.get("url_hint", "")
result = capture_webpage(
url=url,
@@ -499,7 +555,9 @@ def capture():
full_page=full_page,
viewport=viewport,
wait_time=wait_time,
backend=backend
backend=backend,
)
if not result["success"]:

69
manual_capture_guide.md Normal file
View File

@@ -0,0 +1,69 @@
# 手动 Chrome 捕获指南
## 方案三完整流程
### 1⃣ 启动真实 Chrome带调试端口
在您的本地机器或服务器上运行:
```bash
# Linux
google-chrome --remote-debugging-port=9222 https://www.techpowerup.com/gpu-specs/rtx-pro-6000-blackwell.c4272
# 或者使用已有的 Chrome 进程(需要先关闭所有 Chrome
google-chrome-stable --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-debug
```
### 2⃣ 手动验证(如 Cloudflare
- 在浏览器中手动完成验证过程
- 等待页面完全加载
- 确认可以看到正常内容
### 3⃣ 连接到 Chrome 并截图
验证完成后,运行脚本:
```bash
# 连接到 Chrome 并截图
python3.12 /tmp/connect_chrome.py techpowerup
# 或者捕获当前所有页面
python3.12 /tmp/connect_chrome.py
```
### 4⃣ 查看结果
```bash
# 查看截图
ls -lh /tmp/chrome_capture_*.png
# 查看HTML
head -100 /tmp/chrome_html_0.html
```
## API 集成方案
如果需要通过 API 获取内容,可以:
1. 在服务代码中添加 CDP 连接选项
2. 用户先手动验证
3. API 连接到已验证的浏览器实例
## 注意事项
⚠️ **重要**
- Chrome 必须带 `--remote-debugging-port=9222` 启动
- 所有 Chrome 进程必须先关闭(或使用新的 user-data-dir
- 验证完成后才能运行脚本
- 截图后浏览器保持打开,可以继续使用
## 一键脚本
```bash
# 启动 Chrome 并等待用户验证
google-chrome --remote-debugging-port=9222 "https://www.techpowerup.com/gpu-specs/rtx-pro-6000-blackwell.c4272" &
echo "请在浏览器中完成验证,然后按 Enter 继续截图..."
read
python3.12 /tmp/connect_chrome.py techpowerup
```