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.
 
 
 
 
 

71 lines
1.9 KiB

#!/usr/bin/env python3
"""
直接截图到内存,立即发送,不保存任何文件到服务器
"""
import asyncio
from playwright.async_api import async_playwright
import sys
import os
async def screenshot_and_get_bytes(url):
"""
截图并返回字节数据,不保存任何文件
"""
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_direct.py <url>")
sys.exit(1)
url = sys.argv[1]
try:
# 截图到内存
screenshot_bytes = asyncio.run(screenshot_and_get_bytes(url))
# 写入临时文件用于发送
import tempfile
with tempfile.NamedTemporaryFile(suffix='.png', prefix='screenshot_', delete=False) as f:
f.write(screenshot_bytes)
temp_path = f.name
# 输出路径
print(temp_path)
except Exception as e:
print(f"错误: {e}")
import traceback
traceback.print_exc()
sys.exit(1)