Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

价格监控工具

基于图像识别的电商价格监控系统,支持天猫、京东、拼多多、抖音四大平台。

工作原理

  1. 网页截图:使用 Playwright 模拟真实浏览器访问商品页面
  2. 区域裁剪:自动裁剪价格显示区域,提高识别精度
  3. OCR 识别:使用 PaddleOCR 识别图片中的价格数字
  4. 价格存储:将价格历史存入 SQLite 数据库
  5. 变动通知:价格变动时通过邮件/Webhook 等方式通知

系统要求

  • Python 3.8+
  • macOS / Linux / Windows
  • 2GB+ 内存(OCR 需要)

安装步骤

1. 克隆项目并安装依赖

# 创建虚拟环境
python -m venv venv

# 激活虚拟环境
# macOS/Linux:
source venv/bin/activate
# Windows:
# venv\Scripts\activate

# 安装依赖
pip install -r requirements.txt

# 安装 Playwright 浏览器
playwright install chromium

2. 配置文件

cp config.yaml config.yaml.backup
cp config.yaml.example config.yaml

编辑 config.yaml,配置要监控的产品:

products:
  - name: "iPhone 15 Pro Max 256GB"
    platforms:
      - name: "tmall"
        url: "https://detail.tmall.com/item.htm?id=你的商品ID"
      - name: "jd"
        url: "https://item.jd.com/你的商品ID.html"
      - name: "pdd"
        url: "https://mobile.yangkeduo.com/goods.html?goods_id=你的商品ID"
      - name: "douyin"
        url: "https://www.douyin.com/你的商品链接"

3. 运行监控

# 启动定时监控(默认每60分钟检查一次)
python monitor.py

# 只运行一次
python monitor.py --once

# 查看监控状态
python monitor.py --status

# 查看价格历史
python monitor.py --history "iPhone 15 Pro Max 256GB"
python monitor.py --history "iPhone 15 Pro Max 256GB" --platform jd --limit 20

配置说明

监控设置

monitor:
  interval: 60              # 检查间隔(分钟)
  price_change_threshold: 5  # 价格变动阈值,低于此值不通知
  screenshot_timeout: 30     # 截图超时时间(秒)

OCR 设置

ocr:
  engine: "paddleocr"        # OCR 引擎:paddleocr / easyocr / tesseract
  
  # 价格区域裁剪(相对坐标,提高识别精度)
  crop_regions:
    tmall: [0.3, 0.15, 0.7, 0.35]   # [left, top, right, bottom]
    jd: [0.2, 0.15, 0.6, 0.35]
    pdd: [0.2, 0.2, 0.7, 0.4]
    douyin: [0.2, 0.15, 0.6, 0.35]

浏览器设置

browser:
  type: "chromium"           # 浏览器类型
  headless: true             # 无头模式(true=不显示浏览器窗口)
  viewport:
    width: 1280
    height: 800

通知设置

notification:
  enabled: true
  method: "console"          # 通知方式: console / email / webhook / dingtalk
  
  # 邮件通知配置
  email:
    smtp_server: "smtp.gmail.com"
    smtp_port: 587
    username: "你的邮箱"
    password: "你的密码"
    to_addrs: ["收件人邮箱"]
  
  # Webhook 通知
  webhook:
    url: "https://your-webhook-url.com"
    method: "POST"
  
  # 钉钉通知
  dingtalk:
    webhook_url: "https://oapi.dingtalk.com/robot/send?access_token=xxx"
    secret: "你的密钥"

平台适配说明

天猫

  • 页面结构稳定,价格通常在 .tm-price 或 .tm-promo-price 元素中
  • 可能需要登录才能看到真实价格
  • 部分商品有防爬机制

京东

  • 价格通常在 .p-price 或 .J-p-price 元素中
  • 非登录用户可能看到不同价格
  • 秒杀价格需要特殊处理

拼多多

  • 需要特殊处理:拼多多主要面向移动端
  • 系统会自动使用移动端 User-Agent 和视口
  • 价格区域可能需要调整 crop_regions

抖音

  • 页面动态加载内容较多
  • 需要等待 JavaScript 执行完成
  • 直播价格可能与页面价格不同

常见问题

1. OCR 识别不准确

可以调整 config.yaml 中的裁剪区域:

ocr:
  crop_regions:
    tmall: [0.3, 0.15, 0.7, 0.35]  # 根据实际情况调整

格式为 [left, top, right, bottom],值是相对于截图的 0-1 比例。

2. 页面加载失败

  • 检查商品链接是否有效
  • 增加页面加载等待时间:
browser:
  page_load_timeout: 60  # 增加到60秒

3. 被反爬拦截

  • 将 headless 设为 false 观察浏览器行为
  • 增加检查间隔时间
  • 使用代理(需要修改代码支持)

4. PaddleOCR 安装失败

# macOS 可能需要先安装
brew install libomp

# 如果仍有问题,可以尝试其他 OCR 引擎
pip install easyocr
# 然后修改 config.yaml: ocr.engine: "easyocr"

项目结构

.
├── config.yaml          # 配置文件
├── requirements.txt     # 依赖列表
├── monitor.py          # 主程序
├── browser.py          # 浏览器截图模块
├── ocr_engine.py       # OCR 识别模块
├── database.py         # 数据库存储模块
├── notifier.py         # 通知模块
├── utils.py            # 工具函数
├── data/
│   ├── prices.db       # SQLite 数据库
│   ├── screenshots/    # 截图保存目录
│   └── logs/           # 日志文件
└── README.md           # 使用说明

高级用法

自定义通知

修改 notifier.py 添加自定义通知方式:

class MyNotifier(Notifier):
    def notify(self, title: str, message: str, **kwargs):
        # 你的通知逻辑
        pass

批量添加商品

可以编写脚本批量添加商品到配置:

import yaml

with open('config.yaml', 'r') as f:
    config = yaml.safe_load(f)

# 添加新商品
config['products'].append({
    'name': '新商品',
    'platforms': [
        {'name': 'jd', 'url': 'https://item.jd.com/xxx.html'}
    ]
})

with open('config.yaml', 'w') as f:
    yaml.dump(config, f)

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages