本文目录导读:

- 网页批量截屏(Python + Selenium)
- Windows桌面窗口批量截屏(Python + PyAutoGUI)
- MacOS自动截图(AppleScript + Bash)
- 定时批量截屏(Windows PowerShell)
- Linux/Windows通用:当前屏幕定时截屏(Python)
- 安装依赖
- 实用技巧
是的,可以通过脚本批量截屏,以下是几种常见场景的实用脚本方法:
网页批量截屏(Python + Selenium)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
# 配置无头浏览器
chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
# URL列表
urls = [
'https://example.com/page1',
'https://example.com/page2',
# 更多URL...
]
for i, url in enumerate(urls):
driver.get(url)
time.sleep(2) # 等待页面加载
# 全屏截图
driver.save_screenshot(f'screenshot_{i+1}.png')
# 或仅截取特定元素
# element = driver.find_element_by_id('content')
# element.screenshot(f'screenshot_{i+1}.png')
driver.quit()
Windows桌面窗口批量截屏(Python + PyAutoGUI)
import pyautogui
import time
import os
# 保存目录
save_dir = 'screenshots'
os.makedirs(save_dir, exist_ok=True)
# 遍历窗口列表(需要先打开)
windows = [
{'name': '窗口1', 'region': (100, 100, 800, 600)},
{'name': '窗口2', 'region': (200, 200, 800, 600)},
]
for i, win in enumerate(windows):
time.sleep(1) # 切换到目标窗口
# 截取指定区域
screenshot = pyautogui.screenshot(region=win['region'])
screenshot.save(f'{save_dir}/{win["name"]}.png')
print(f'已保存: {win["name"]}')
MacOS自动截图(AppleScript + Bash)
#!/bin/bash
# 批量截图脚本(Mac)
screenshot_dir="$HOME/Desktop/screenshots"
mkdir -p "$screenshot_dir"
列表s=(
"Safari"
"Terminal"
"Finder"
)
in "${titles[@]}"; do
# 激活窗口
osascript -e "tell application \"$title\" to activate"
sleep 1
# 截图(macOS自带screencapture)
screencapture -w "$screenshot_dir/$title.png"
echo "已截图: $title"
done
定时批量截屏(Windows PowerShell)
# 定时截屏脚本
$duration = 60 # 总时长(秒)
$interval = 10 # 间隔(秒)
$outputFolder = "C:\Screenshots"
New-Item -ItemType Directory -Force -Path $outputFolder
for ($i=0; $i -lt ($duration/$interval); $i++) {
$filename = "screenshot_$(Get-Date -Format 'yyyyMMdd_HHmmss').png"
$path = Join-Path $outputFolder $filename
# 使用.NET截屏
Add-Type -AssemblyName System.Drawing
$screen = [System.Windows.Forms.Screen]::PrimaryScreen
$bounds = $screen.Bounds
$bitmap = New-Object System.Drawing.Bitmap $bounds.Width, $bounds.Height
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.CopyFromScreen($bounds.X, $bounds.Y, 0, 0, $bounds.Size)
$bitmap.Save($path, [System.Drawing.Imaging.ImageFormat]::Png)
$graphics.Dispose()
$bitmap.Dispose()
Start-Sleep -Seconds $interval
}
Linux/Windows通用:当前屏幕定时截屏(Python)
import pyautogui
import time
from datetime import datetime
import os
def batch_screenshots(interval=5, count=10, output_dir='screenshots'):
"""批量截屏函数"""
os.makedirs(output_dir, exist_ok=True)
for i in range(count):
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
filename = f'screenshot_{timestamp}.png'
filepath = os.path.join(output_dir, filename)
# 截取整个屏幕
screenshot = pyautogui.screenshot()
screenshot.save(filepath)
print(f'已保存: {filepath}')
if i < count - 1: # 最后一次不需要等待
time.sleep(interval)
# 使用示例:每5秒截一次,共10次
batch_screenshots(interval=5, count=10)
安装依赖
# Python环境 pip install pyautogui pillow selenium # Windows需额外安装 # .NET Framework(通常已安装)
实用技巧
- 增量命名:使用时间戳避免覆盖
- 错误处理:添加try-except跳过失败的截图
- 多线程:提高批量处理速度
- 参数化:通过命令行参数控制间隔、数量等
选择哪种脚本取决于你的具体需求(网页、桌面、定时等),需要哪种场景的完整实现?