优化响应速度

This commit is contained in:
fuzhongyun 2026-03-30 17:59:46 +08:00
parent 1bd1139c1d
commit 243ce9c03c
1 changed files with 44 additions and 1 deletions

45
main.py
View File

@ -13,6 +13,8 @@ FINGERPRINT_HEADER = "x-device-fingerprint"
playwright_instance = None playwright_instance = None
browser = None browser = None
# [新增] 全局内存缓存池,存储 JS 脚本
RESOURCE_CACHE = {}
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
@ -43,6 +45,44 @@ app = FastAPI(
async def get_fingerprint(): async def get_fingerprint():
page = await browser.new_page() page = await browser.new_page()
try: 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() fingerprint_future = asyncio.Future()
async def handle_request(request): async def handle_request(request):
@ -52,7 +92,10 @@ async def get_fingerprint():
fingerprint_future.set_result(headers[FINGERPRINT_HEADER]) fingerprint_future.set_result(headers[FINGERPRINT_HEADER])
page.on("request", handle_request) 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) fingerprint = await asyncio.wait_for(fingerprint_future, timeout=15)
return fingerprint return fingerprint
except asyncio.TimeoutError: except asyncio.TimeoutError: