Commit ·
f63c37f
1
Parent(s): a374db7
fix: 无头模式下代理不生效
Browse files3 unmodified lines45678910111213141544 unmodified lines6061626364656667683 unmodified lines"""import asyncioimport timefrom typing import Optionalfrom playwright.async_api import async_playwright, Browser, BrowserContextfrom ..core.logger import debug_loggerclass BrowserCaptchaService:"""浏览器自动化获取 reCAPTCHA token(单例模式)"""44 unmodified lines]}# 如果有代理,添加代理配置if proxy_url:launch_options['proxy'] = {'server': proxy_url}self.browser = await self.playwright.chromium.launch(**launch_options)self._initialized = True3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394044 unmodified lines8586878889909192939495969798993 unmodified lines"""import asyncioimport timeimport refrom typing import Optional, Dictfrom playwright.async_api import async_playwright, Browser, BrowserContextfrom ..core.logger import debug_loggerdef parse_proxy_url(proxy_url: str) -> Optional[Dict[str, str]]:"""解析代理URL,分离协议、主机、端口、认证信息Args:proxy_url: 代理URL,格式:protocol://[username:password@]host:portReturns:代理配置字典,包含server、username、password(如果有认证)"""proxy_pattern = r'^(socks5|http|https)://(?:([^:]+):([^@]+)@)?([^:]+):(\d+)$'match = re.match(proxy_pattern, proxy_url)if match:protocol, username, password, host, port = match.groups()proxy_config = {'server': f'{protocol}://{host}:{port}'}if username and password:proxy_config['username'] = usernameproxy_config['password'] = passwordreturn proxy_configreturn Noneclass BrowserCaptchaService:"""浏览器自动化获取 reCAPTCHA token(单例模式)"""44 unmodified lines]}# 如果有代理,解析并添加代理配置if proxy_url:proxy_config = parse_proxy_url(proxy_url)if proxy_config:launch_options['proxy'] = proxy_configauth_info = "auth=yes" if 'username' in proxy_config else "auth=no"debug_logger.log_info(f"[BrowserCaptcha] 代理配置: {proxy_config['server']} ({auth_info})")else:debug_logger.log_warning(f"[BrowserCaptcha] 代理URL格式错误: {proxy_url}")self.browser = await self.playwright.chromium.launch(**launch_options)self._initialized = True
src/services/browser_captcha.py+34-3