headPicker/capture_fingerprint.py

50 lines
1.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
from playwright.async_api import async_playwright
TARGET_PAGE = "https://gd.10086.cn/gdshop/qdxsd/index.html#/gd-fls/marketingActivity/index?id=1956241557401346048"
TARGET_API = "https://gd.10086.cn/gdshop/apigw/adv/ad/getInsertCode"
FINGERPRINT_HEADER = "x-device-fingerprint"
async def capture_fingerprint():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
page = await browser.new_page()
fingerprint_future = asyncio.Future()
async def handle_request(request):
if TARGET_API in request.url and not fingerprint_future.done():
headers = request.headers
if FINGERPRINT_HEADER in headers:
fingerprint = headers[FINGERPRINT_HEADER]
fingerprint_future.set_result(fingerprint)
page.on("request", handle_request)
print(f"[信息] 正在打开页面...")
await page.goto(TARGET_PAGE, wait_until="networkidle")
try:
print(f"[信息] 等待指纹参数...")
fingerprint = await asyncio.wait_for(fingerprint_future, timeout=15)
print(f"\n{'='*60}")
print(f"[成功] 指纹参数捕获成功:")
print(f" {FINGERPRINT_HEADER}: {fingerprint}")
print(f"{'='*60}\n")
return fingerprint
except asyncio.TimeoutError:
print(f"\n{'='*60}")
print(f"[错误] 等待超时,未能捕获到 {FINGERPRINT_HEADER} 参数")
print(f"{'='*60}\n")
return None
finally:
await browser.close()
if __name__ == "__main__":
asyncio.run(capture_fingerprint())