Skip to content

StruggleYang/screenshotsApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Web Screenshot Service

一个基于 Flask 和 Selenium 的网页截图服务,支持批量截图并自动上传到 Cloudflare R2 存储。

功能特性

  • 🌐 网页截图: 使用 Chrome 浏览器自动截取网页截图
  • �� 批量处理: 支持多个 URL 批量截图
  • 🖼️ 图像优化: 自动压缩并转换为 WebP 格式
  • ☁️ 云端存储: 自动上传到 Cloudflare R2 存储
  • 🔐 身份验证: Basic Auth 认证保护
  • �� 跨平台: 支持 Linux 和 Windows/macOS 系统
  • ⚙️ 可配置: 支持自定义窗口大小、等待时间等参数

系统要求

  • Python 3.7+
  • Chrome 浏览器
  • 足够的磁盘空间用于临时截图存储

安装

1. 克隆仓库

git clone https://github.com/StruggleYang/screenshotsApp
cd screenshotsApp

2. 安装依赖

pip install -r requirements.txt

3. 创建 requirements.txt

如果还没有 requirements.txt 文件,请创建包含以下依赖的文件:

Flask==2.3.3
boto3==1.28.0
selenium==4.15.0
webdriver-manager==4.0.1
Pillow==10.0.1

4. 创建必要的目录

mkdir screenshots

环境配置

必需的环境变量

.env 文件中配置以下环境变量:

# 应用认证
APP_USERNAME=your_username
APP_PASSWORD=your_password

# Cloudflare R2 配置
R2_ACCESS_KEY=your_r2_access_key
R2_SECRET_KEY=your_r2_secret_key
R2_BUCKET_NAME=your_bucket_name
R2_ENDPOINT=https://your-account-id.r2.cloudflarestorage.com

# Linux 系统 Chrome 驱动路径(可选)
webdriver_chrome_driver=/path/to/chromedriver

获取 Cloudflare R2 配置

  1. 登录 Cloudflare Dashboard
  2. 进入 R2 对象存储
  3. 创建存储桶并获取访问密钥
  4. 记录账户 ID 和存储桶名称

使用方法

启动服务

python screenshotsApp.py

服务将在 http://localhost:8080 启动。

API 接口

健康检查

curl http://localhost:8080/

截图接口

POST /screenshot

请求头:

Authorization: Basic <base64_encoded_credentials>
Content-Type: application/json

请求体:

{
  "urls": "https://example.com,https://google.com",
  "show_window": false,
  "sleep_time": 10
}

参数说明:

  • urls: 要截图的 URL 列表,用逗号分隔
  • show_window: 是否显示浏览器窗口(默认 false)
  • sleep_time: 页面加载等待时间,单位秒(默认 10)

示例请求:

curl -X POST http://localhost:8080/screenshot \
  -H "Authorization: Basic $(echo -n 'username:password' | base64)" \
  -H "Content-Type: application/json" \
  -d '{
    "urls": "https://www.google.com,https://github.com",
    "show_window": false,
    "sleep_time": 15
  }'

响应示例:

{
  "message": "所有 URL 的截图已成功上传!"
}

文件命名规则

截图文件会根据 URL 自动生成文件名:

  • 移除协议前缀(http://, https://)
  • 移除 www. 前缀
  • 将特殊字符替换为连字符
  • 转换为小写
  • 添加 .webp 扩展名

示例:

  • https://www.google.comscreenshot_google-com.webp
  • https://play.google.com/store/apps/details?id=com.example.appscreenshot_google-play-com-example-app.webp

部署

Docker 部署

创建 Dockerfile:

FROM python:3.9-slim

# 安装 Chrome 和依赖
RUN apt-get update && apt-get install -y \
    wget \
    gnupg \
    unzip \
    && wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list \
    && apt-get update \
    && apt-get install -y google-chrome-stable \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY screenshotsApp.py .
RUN mkdir screenshots

EXPOSE 8080

CMD ["python", "screenshotsApp.py"]

构建和运行:

docker build -t screenshot-service .
docker run -p 8080:8080 --env-file .env screenshot-service

生产环境部署

建议使用 Gunicorn 作为 WSGI 服务器:

pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:8080 screenshotsApp:app

故障排除

常见问题

  1. Chrome 驱动问题

    • 确保 Chrome 浏览器已安装
    • Linux 系统需要设置 webdriver_chrome_driver 环境变量
  2. 权限问题

    • 确保 screenshots 目录有写入权限
    • 检查 R2 访问密钥是否正确
  3. 内存不足

    • 减少并发请求数量
    • 增加系统内存或使用 swap
  4. 网络超时

    • 增加 sleep_time 参数
    • 检查网络连接

日志查看

服务会输出详细的日志信息,包括:

  • 截图保存路径
  • 上传状态
  • 错误信息

安全注意事项

  • 使用强密码保护 API 接口
  • 定期轮换 R2 访问密钥
  • 限制服务访问来源
  • 监控异常请求

贡献

欢迎提交 Issue 和 Pull Request!

更新日志

v1.0.0

  • 初始版本发布
  • 支持批量网页截图
  • 集成 Cloudflare R2 存储
  • 支持 WebP 格式转换

About

https://aiwith.me Web Screenshot Script File

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages