From 0c05b260eae0670223f4df2b7c6e1561d9d7167d Mon Sep 17 00:00:00 2001 From: SayItDitto <123747090+SayItDitto@users.noreply.github.com> Date: Sun, 23 Aug 2026 09:11:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20CloakBrowser=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E6=BA=90=E7=A0=81=E5=9C=A8=20CF=20=E8=B4=A8?= =?UTF-8?q?=E8=AF=A2=E9=A1=B5=E8=B6=85=E6=97=B6=20(#6402)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/adapters/network/browser.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/app/adapters/network/browser.py b/app/adapters/network/browser.py index 458424407..ae6a4c1a2 100644 --- a/app/adapters/network/browser.py +++ b/app/adapters/network/browser.py @@ -1118,6 +1118,7 @@ class PlaywrightHelper: :param headless: 是否无头模式 :param timeout: 超时时间 """ + timeout = timeout or 60 source = None # 如果配置为 FlareSolverr,则直接调用获取页面源码 if self.__browser_emulation() == "flaresolverr": @@ -1140,10 +1141,33 @@ class PlaywrightHelper: if cookies: page.set_extra_http_headers({"cookie": cookies}) - page.goto(url) - page.wait_for_load_state("networkidle", timeout=timeout * 1000) + page.goto(url, wait_until="load", timeout=timeout * 1000) - source = page.content() + # 修复: 部分站点(如 Cloudflare 质询页)会持续轮询请求, + # 导致 networkidle 永不触发而超时。改为等待页面加载完成后 + # 轮询检查标题, 直到不再停留在质询/加载页。 + challenge_titles = ("just a moment", "请稍候", "loading") + deadline = time.time() + timeout + while time.time() < deadline: + try: + current_title = (page.title() or "").strip().lower() + except Exception: + current_title = "" + if current_title and not any( + t in current_title for t in challenge_titles): + break + time.sleep(2) + + # 页面跳转中 content() 可能失败, 重试几次 + source = None + for _attempt in range(5): + try: + source = page.content() + if source: + break + except Exception: + source = None + time.sleep(2) except Exception as e: logger.error(f"获取网页源码失败: {str(e)}")