Spaces:
Running
Running
File size: 1,194 Bytes
6fff8e3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # 用无头浏览器截取指定时间点的画面:python3 shot.py out.png "t1,t2,..." [l|p] [width]
import sys, asyncio
from playwright.async_api import async_playwright
async def main():
out, ts = sys.argv[1], sys.argv[2]
o = sys.argv[3] if len(sys.argv) > 3 else 'l'
w = sys.argv[4] if len(sys.argv) > 4 else '960'
base = sys.argv[5] if len(sys.argv) > 5 else 'http://localhost:5173/'
async with async_playwright() as p:
b = await p.chromium.launch(args=["--use-gl=swiftshader", "--enable-unsafe-swiftshader"])
pg = await b.new_page(viewport={"width": 2000, "height": 1200})
logs = []
pg.on("console", lambda m: logs.append(m.text) if m.type in ("error", "warning") else None)
pg.on("pageerror", lambda e: logs.append("PAGEERROR " + str(e)))
await pg.goto(f"{base}?still={ts}&o={o}&w={w}")
try:
await pg.wait_for_function("document.title==='ready'", timeout=90000)
except Exception as e:
print("timeout", e)
el = await pg.query_selector("canvas")
if el: await el.screenshot(path=out)
for l in logs[:15]: print(l)
await b.close()
asyncio.run(main())
|