From 243ce9c03cfc67c632eadc220305d560e3bec778 Mon Sep 17 00:00:00 2001 From: fuzhongyun <15339891972@163.com> Date: Mon, 30 Mar 2026 17:59:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=93=8D=E5=BA=94=E9=80=9F?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index d47d7bb..a2ca03b 100644 --- a/main.py +++ b/main.py @@ -13,6 +13,8 @@ FINGERPRINT_HEADER = "x-device-fingerprint" playwright_instance = None browser = None +# [新增] 全局内存缓存池,存储 JS 脚本 +RESOURCE_CACHE = {} @asynccontextmanager async def lifespan(app: FastAPI): @@ -43,6 +45,44 @@ app = FastAPI( async def get_fingerprint(): page = await browser.new_page() try: + # [新增] 拦截无用资源,极大提升加载速度 + async def route_intercept(route): + request = route.request + url = request.url + resource_type = request.resource_type + + # 1. 丢弃无用资源 + if resource_type in ["image", "stylesheet", "media", "font", "other"]: + await route.abort() + return + + # 2. 强缓存 JS 文件 + if resource_type == "script": + if url in RESOURCE_CACHE: + await route.fulfill( + status=200, + headers=RESOURCE_CACHE[url]["headers"], + body=RESOURCE_CACHE[url]["body"] + ) + return + + # 缓存未命中,去真实抓取 + try: + response = await route.fetch() + body = await response.body() + RESOURCE_CACHE[url] = { + "headers": response.headers, + "body": body + } + await route.fulfill(response=response, body=body) + return + except Exception: + pass # 抓取失败,降级给底层处理 + + await route.continue_() + + await page.route("**/*", route_intercept) + fingerprint_future = asyncio.Future() async def handle_request(request): @@ -52,7 +92,10 @@ async def get_fingerprint(): fingerprint_future.set_result(headers[FINGERPRINT_HEADER]) page.on("request", handle_request) - await page.goto(TARGET_PAGE, wait_until="networkidle") + + # [修改] 并发执行 goto 和 wait_for。拿到结果就立刻返回,不再死等 goto 结束 + goto_task = asyncio.create_task(page.goto(TARGET_PAGE, wait_until="domcontentloaded")) + fingerprint = await asyncio.wait_for(fingerprint_future, timeout=15) return fingerprint except asyncio.TimeoutError: