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: