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.
74 lines
2.0 KiB
74 lines
2.0 KiB
#!/usr/bin/env python3
|
|
"""
|
|
截图并直接发送,不保存文件到服务器
|
|
"""
|
|
|
|
import asyncio
|
|
from playwright.async_api import async_playwright
|
|
import sys
|
|
import tempfile
|
|
import os
|
|
|
|
async def screenshot_and_send(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",
|
|
"--disable-software-rasterizer",
|
|
"--disable-extensions",
|
|
"--no-first-run",
|
|
"--no-default-browser-check",
|
|
]
|
|
)
|
|
|
|
# 创建页面
|
|
page = await browser.new_page(
|
|
viewport={"width": 1920, "height": 1080}
|
|
)
|
|
|
|
# 访问网站
|
|
await page.goto(url, wait_until="commit", timeout=30000)
|
|
|
|
# 截图到内存
|
|
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_and_send.py <url>")
|
|
sys.exit(1)
|
|
|
|
url = sys.argv[1]
|
|
|
|
try:
|
|
# 截图
|
|
screenshot_bytes = asyncio.run(screenshot_and_send(url))
|
|
|
|
# 保存到临时文件用于发送
|
|
with tempfile.NamedTemporaryFile(suffix='.png', prefix='screenshot_', delete=False) as f:
|
|
f.write(screenshot_bytes)
|
|
temp_path = f.name
|
|
|
|
print(f"临时文件: {temp_path}")
|
|
print(temp_path) # 输出路径供调用者使用
|
|
|
|
except Exception as e:
|
|
print(f"错误: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
sys.exit(1)
|
|
|