You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

65 lines
1.7 KiB

#!/usr/bin/env python3
"""
截图到内存,直接发送,不保存文件到服务器
"""
import asyncio
from playwright.async_api import async_playwright
import sys
import base64
async def screenshot_to_memory(url):
"""
截图到内存,返回 base64
"""
print(f"正在打开: {url}")
async with async_playwright() as p:
# 启动浏览器
browser = await p.chromium.launch(
headless=True,
args=[
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
]
)
# 创建页面
page = await browser.new_page(
viewport={"width": 1920, "height": 1080}
)
# 访问网站
await page.goto(url, wait_until="load", timeout=30000)
await asyncio.sleep(2)
# 截图到内存
screenshot_bytes = await page.screenshot(full_page=False)
print(f"截图完成,大小: {len(screenshot_bytes)} bytes")
# 关闭浏览器
await browser.close()
return screenshot_bytes
if __name__ == "__main__":
if len(sys.argv) < 2:
print("使用方法: python screenshot_no_save.py <url>")
sys.exit(1)
url = sys.argv[1]
try:
# 截图到内存
screenshot_bytes = asyncio.run(screenshot_to_memory(url))
# 输出 base64
print(base64.b64encode(screenshot_bytes).decode('utf-8'))
except Exception as e:
print(f"错误: {e}")
import traceback
traceback.print_exc()
sys.exit(1)