genz27 Warp commited on
Commit ·
260483d
1
Parent(s): 5817830
refactor: 将有头浏览器打码从 patchright 改为 playwright
Browse files- browser_captcha.py: 所有 patchright 引用改为 playwright
- flow_client.py: 更新错误提示信息
- requirements.txt: patchright 改为 playwright
Co-Authored-By: Warp <agent@warp.dev>
- requirements.txt +1 -1
- src/services/browser_captcha.py +27 -27
- src/services/flow_client.py +2 -2
requirements.txt
CHANGED
|
@@ -7,5 +7,5 @@ tomli==2.2.1
|
|
| 7 |
bcrypt==4.2.1
|
| 8 |
python-multipart==0.0.20
|
| 9 |
python-dateutil==2.8.2
|
| 10 |
-
|
| 11 |
nodriver>=0.48.0
|
|
|
|
| 7 |
bcrypt==4.2.1
|
| 8 |
python-multipart==0.0.20
|
| 9 |
python-dateutil==2.8.2
|
| 10 |
+
playwright>=1.40.0
|
| 11 |
nodriver>=0.48.0
|
src/services/browser_captcha.py
CHANGED
|
@@ -5,7 +5,7 @@
|
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
import subprocess
|
| 8 |
-
# 修复 Windows 上
|
| 9 |
os.environ.setdefault("PLAYWRIGHT_BROWSERS_PATH", "0")
|
| 10 |
|
| 11 |
import asyncio
|
|
@@ -43,7 +43,7 @@ def _is_running_in_docker() -> bool:
|
|
| 43 |
IS_DOCKER = _is_running_in_docker()
|
| 44 |
|
| 45 |
|
| 46 |
-
# ====================
|
| 47 |
def _run_pip_install(package: str, use_mirror: bool = False) -> bool:
|
| 48 |
"""运行 pip install 命令"""
|
| 49 |
cmd = [sys.executable, '-m', 'pip', 'install', package]
|
|
@@ -67,8 +67,8 @@ def _run_pip_install(package: str, use_mirror: bool = False) -> bool:
|
|
| 67 |
|
| 68 |
|
| 69 |
def _run_playwright_install(use_mirror: bool = False) -> bool:
|
| 70 |
-
"""安装
|
| 71 |
-
cmd = [sys.executable, '-m', '
|
| 72 |
env = os.environ.copy()
|
| 73 |
|
| 74 |
if use_mirror:
|
|
@@ -91,37 +91,37 @@ def _run_playwright_install(use_mirror: bool = False) -> bool:
|
|
| 91 |
return False
|
| 92 |
|
| 93 |
|
| 94 |
-
def
|
| 95 |
-
"""确保
|
| 96 |
try:
|
| 97 |
-
import
|
| 98 |
-
debug_logger.log_info("[BrowserCaptcha]
|
| 99 |
return True
|
| 100 |
except ImportError:
|
| 101 |
pass
|
| 102 |
|
| 103 |
-
debug_logger.log_info("[BrowserCaptcha]
|
| 104 |
-
print("[BrowserCaptcha]
|
| 105 |
|
| 106 |
# 先尝试官方源
|
| 107 |
-
if _run_pip_install('
|
| 108 |
return True
|
| 109 |
|
| 110 |
# 官方源失败,尝试国内镜像
|
| 111 |
debug_logger.log_info("[BrowserCaptcha] 官方源安装失败,尝试国内镜像...")
|
| 112 |
print("[BrowserCaptcha] 官方源安装失败,尝试国内镜像...")
|
| 113 |
-
if _run_pip_install('
|
| 114 |
return True
|
| 115 |
|
| 116 |
-
debug_logger.log_error("[BrowserCaptcha] ❌
|
| 117 |
-
print("[BrowserCaptcha] ❌
|
| 118 |
return False
|
| 119 |
|
| 120 |
|
| 121 |
def _ensure_browser_installed() -> bool:
|
| 122 |
"""确保 chromium 浏览器已安装"""
|
| 123 |
try:
|
| 124 |
-
from
|
| 125 |
with sync_playwright() as p:
|
| 126 |
# 尝试获取浏览器路径,如果失败说明未安装
|
| 127 |
browser_path = p.chromium.executable_path
|
|
@@ -144,31 +144,31 @@ def _ensure_browser_installed() -> bool:
|
|
| 144 |
if _run_playwright_install(use_mirror=True):
|
| 145 |
return True
|
| 146 |
|
| 147 |
-
debug_logger.log_error("[BrowserCaptcha] ❌ chromium 浏览器自动安装失败,请手动安装: python -m
|
| 148 |
-
print("[BrowserCaptcha] ❌ chromium 浏览器自动安装失败,请手动安装: python -m
|
| 149 |
return False
|
| 150 |
|
| 151 |
|
| 152 |
-
# 尝试导入
|
| 153 |
async_playwright = None
|
| 154 |
Route = None
|
| 155 |
BrowserContext = None
|
| 156 |
-
|
| 157 |
|
| 158 |
if IS_DOCKER:
|
| 159 |
debug_logger.log_warning("[BrowserCaptcha] 检测到 Docker 环境,有头浏览器打码不可用,请使用第三方打码服务")
|
| 160 |
print("[BrowserCaptcha] ⚠️ 检测到 Docker 环境,有头浏览器打码不可用")
|
| 161 |
print("[BrowserCaptcha] 请使用第三方打码服务: yescaptcha, capmonster, ezcaptcha, capsolver")
|
| 162 |
else:
|
| 163 |
-
if
|
| 164 |
try:
|
| 165 |
-
from
|
| 166 |
-
|
| 167 |
# 检查并安装浏览器
|
| 168 |
_ensure_browser_installed()
|
| 169 |
except ImportError as e:
|
| 170 |
-
debug_logger.log_error(f"[BrowserCaptcha]
|
| 171 |
-
print(f"[BrowserCaptcha] ❌
|
| 172 |
|
| 173 |
|
| 174 |
# 配置
|
|
@@ -563,10 +563,10 @@ class BrowserCaptchaService:
|
|
| 563 |
"有头浏览器打码在 Docker 环境中不可用。"
|
| 564 |
"请使用第三方打码服务: yescaptcha, capmonster, ezcaptcha, capsolver"
|
| 565 |
)
|
| 566 |
-
if not
|
| 567 |
raise RuntimeError(
|
| 568 |
-
"
|
| 569 |
-
"请手动安装: pip install
|
| 570 |
)
|
| 571 |
|
| 572 |
async def _load_browser_count(self):
|
|
|
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
import subprocess
|
| 8 |
+
# 修复 Windows 上 playwright 的 asyncio 兼容性问题
|
| 9 |
os.environ.setdefault("PLAYWRIGHT_BROWSERS_PATH", "0")
|
| 10 |
|
| 11 |
import asyncio
|
|
|
|
| 43 |
IS_DOCKER = _is_running_in_docker()
|
| 44 |
|
| 45 |
|
| 46 |
+
# ==================== playwright 自动安装 ====================
|
| 47 |
def _run_pip_install(package: str, use_mirror: bool = False) -> bool:
|
| 48 |
"""运行 pip install 命令"""
|
| 49 |
cmd = [sys.executable, '-m', 'pip', 'install', package]
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
def _run_playwright_install(use_mirror: bool = False) -> bool:
|
| 70 |
+
"""安装 playwright chromium 浏览器"""
|
| 71 |
+
cmd = [sys.executable, '-m', 'playwright', 'install', 'chromium']
|
| 72 |
env = os.environ.copy()
|
| 73 |
|
| 74 |
if use_mirror:
|
|
|
|
| 91 |
return False
|
| 92 |
|
| 93 |
|
| 94 |
+
def _ensure_playwright_installed() -> bool:
|
| 95 |
+
"""确保 playwright 已安装"""
|
| 96 |
try:
|
| 97 |
+
import playwright
|
| 98 |
+
debug_logger.log_info("[BrowserCaptcha] playwright 已安装")
|
| 99 |
return True
|
| 100 |
except ImportError:
|
| 101 |
pass
|
| 102 |
|
| 103 |
+
debug_logger.log_info("[BrowserCaptcha] playwright 未安装,开始自动安装...")
|
| 104 |
+
print("[BrowserCaptcha] playwright 未安装,开始自动安装...")
|
| 105 |
|
| 106 |
# 先尝试官方源
|
| 107 |
+
if _run_pip_install('playwright', use_mirror=False):
|
| 108 |
return True
|
| 109 |
|
| 110 |
# 官方源失败,尝试国内镜像
|
| 111 |
debug_logger.log_info("[BrowserCaptcha] 官方源安装失败,尝试国内镜像...")
|
| 112 |
print("[BrowserCaptcha] 官方源安装失败,尝试国内镜像...")
|
| 113 |
+
if _run_pip_install('playwright', use_mirror=True):
|
| 114 |
return True
|
| 115 |
|
| 116 |
+
debug_logger.log_error("[BrowserCaptcha] ❌ playwright 自动安装失败,请手动安装: pip install playwright")
|
| 117 |
+
print("[BrowserCaptcha] ❌ playwright 自动安装失败,请手动安装: pip install playwright")
|
| 118 |
return False
|
| 119 |
|
| 120 |
|
| 121 |
def _ensure_browser_installed() -> bool:
|
| 122 |
"""确保 chromium 浏览器已安装"""
|
| 123 |
try:
|
| 124 |
+
from playwright.sync_api import sync_playwright
|
| 125 |
with sync_playwright() as p:
|
| 126 |
# 尝试获取浏览器路径,如果失败说明未安装
|
| 127 |
browser_path = p.chromium.executable_path
|
|
|
|
| 144 |
if _run_playwright_install(use_mirror=True):
|
| 145 |
return True
|
| 146 |
|
| 147 |
+
debug_logger.log_error("[BrowserCaptcha] ❌ chromium 浏览器自动安装失败,请手动安装: python -m playwright install chromium")
|
| 148 |
+
print("[BrowserCaptcha] ❌ chromium 浏览器自动安装失败,请手动安装: python -m playwright install chromium")
|
| 149 |
return False
|
| 150 |
|
| 151 |
|
| 152 |
+
# 尝试导入 playwright
|
| 153 |
async_playwright = None
|
| 154 |
Route = None
|
| 155 |
BrowserContext = None
|
| 156 |
+
PLAYWRIGHT_AVAILABLE = False
|
| 157 |
|
| 158 |
if IS_DOCKER:
|
| 159 |
debug_logger.log_warning("[BrowserCaptcha] 检测到 Docker 环境,有头浏览器打码不可用,请使用第三方打码服务")
|
| 160 |
print("[BrowserCaptcha] ⚠️ 检测到 Docker 环境,有头浏览器打码不可用")
|
| 161 |
print("[BrowserCaptcha] 请使用第三方打码服务: yescaptcha, capmonster, ezcaptcha, capsolver")
|
| 162 |
else:
|
| 163 |
+
if _ensure_playwright_installed():
|
| 164 |
try:
|
| 165 |
+
from playwright.async_api import async_playwright, Route, BrowserContext
|
| 166 |
+
PLAYWRIGHT_AVAILABLE = True
|
| 167 |
# 检查并安装浏览器
|
| 168 |
_ensure_browser_installed()
|
| 169 |
except ImportError as e:
|
| 170 |
+
debug_logger.log_error(f"[BrowserCaptcha] playwright 导入失败: {e}")
|
| 171 |
+
print(f"[BrowserCaptcha] ❌ playwright 导入失败: {e}")
|
| 172 |
|
| 173 |
|
| 174 |
# 配置
|
|
|
|
| 563 |
"有头浏览器打码在 Docker 环境中不可用。"
|
| 564 |
"请使用第三方打码服务: yescaptcha, capmonster, ezcaptcha, capsolver"
|
| 565 |
)
|
| 566 |
+
if not PLAYWRIGHT_AVAILABLE or async_playwright is None:
|
| 567 |
raise RuntimeError(
|
| 568 |
+
"playwright 未安装或不可用。"
|
| 569 |
+
"请手动安装: pip install playwright && python -m playwright install chromium"
|
| 570 |
)
|
| 571 |
|
| 572 |
async def _load_browser_count(self):
|
src/services/flow_client.py
CHANGED
|
@@ -1183,7 +1183,7 @@ class FlowClient:
|
|
| 1183 |
except Exception as e:
|
| 1184 |
debug_logger.log_error(f"[reCAPTCHA Personal] 错误: {str(e)}")
|
| 1185 |
return None, None
|
| 1186 |
-
# 有头浏览器打码 (
|
| 1187 |
elif captcha_method == "browser":
|
| 1188 |
try:
|
| 1189 |
from .browser_captcha import BrowserCaptchaService
|
|
@@ -1197,7 +1197,7 @@ class FlowClient:
|
|
| 1197 |
return None, None
|
| 1198 |
except ImportError as e:
|
| 1199 |
debug_logger.log_error(f"[reCAPTCHA Browser] 导入失败: {str(e)}")
|
| 1200 |
-
print(f"[reCAPTCHA] ❌
|
| 1201 |
return None, None
|
| 1202 |
except Exception as e:
|
| 1203 |
debug_logger.log_error(f"[reCAPTCHA Browser] 错误: {str(e)}")
|
|
|
|
| 1183 |
except Exception as e:
|
| 1184 |
debug_logger.log_error(f"[reCAPTCHA Personal] 错误: {str(e)}")
|
| 1185 |
return None, None
|
| 1186 |
+
# 有头浏览器打码 (playwright)
|
| 1187 |
elif captcha_method == "browser":
|
| 1188 |
try:
|
| 1189 |
from .browser_captcha import BrowserCaptchaService
|
|
|
|
| 1197 |
return None, None
|
| 1198 |
except ImportError as e:
|
| 1199 |
debug_logger.log_error(f"[reCAPTCHA Browser] 导入失败: {str(e)}")
|
| 1200 |
+
print(f"[reCAPTCHA] ❌ playwright 未安装,请运行: pip install playwright && python -m playwright install chromium")
|
| 1201 |
return None, None
|
| 1202 |
except Exception as e:
|
| 1203 |
debug_logger.log_error(f"[reCAPTCHA Browser] 错误: {str(e)}")
|