🎯 竞争对手猎人
AI驱动的竞争对手分析代理 |使用MCP、LangGraph和Playwright进行自动网页抓取和结构化数据提取
  
______________________________________________________________________
📖 引言
竞争对手猎人 是一个生产就绪的人工智能代理,通过抓取产品页面和使用大型语言模型提取结构化信息来自动化竞争对手分析。建立在 模型上下文协议(MCP),它与Claude Desktop和其他MCP兼容客户端无缝集成。
关键能力
- 🔍 智能网页抓取:具有反检测功能的基于浏览器的自动内容提取
- 🤖 LLM动力提取:使用OpenAI兼容API进行结构化数据提取
- 📊 结构化输出:Pydantic验证的产品信息(定价、功能、SWOT分析)
- 🔄 LangGraph工作流:强大的状态管理和错误处理
- 🔌 MCP集成:对Claude Desktop和MCP客户端的原生支持
______________________________________________________________________
🏗️ 建筑
系统如下 六边形架构 关注点明确分离。工作流:用户请求→ MCP服务器→ LangGraph工作流→ 浏览器抓取→ LLM提取→ 结构化数据响应。
______________________________________________________________________
✨ 核心功能
- 🤖 AI驱动:使用LLM进行智能提取,并自动进行SWOT分析
- 📊 结构化输出:Pydantic验证的数据模型(定价、功能、摘要)
- 🛡️ 反检测:随机用户代理、智能滚动、自动截图
- 🔌 MCP本地:与Claude Desktop和Cursor IDE无缝集成
- 📦 CLI工具:通过专业命令行界面
competitor-hunter命令 - 异步/等待:完全异步编程,实现最佳性能
______________________________________________________________________
🚀 快速开始
先决条件
- Python 3.10+ (推荐3.11或3.12)
- 紫外线 或 诗歌 (依赖关系管理器)
- 剧作家 浏览器(自动安装)
安装
- 克隆仓库:
git clone https://github.com/your-username/competitor-hunter.git
cd competitor-hunter- 安装依赖项 (使用紫外线):
uv sync或者使用诗歌:
poetry install或者使用pip:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -e ".[dev]"- 安装Playwright浏览器:
playwright install chromium配置
创建一个 .env 项目根目录中的文件:
# OpenAI API Configuration
OPENAI_API_KEY=your_openai_api_key_here
OPENAI_BASE_URL=https://api.openai.com/v1 # Optional: for custom endpoints
OPENAI_MODEL_NAME=gpt-4o # Optional: default is gpt-4o
# Browser Configuration
HEADLESS_MODE=true # Set to false for debugging
# Database Configuration
DB_PATH=data/competitors.db # SQLite database path💡 小贴士:复制 .env.example 到 .env 并填写您的值:
cp .env.example .env______________________________________________________________________
📸 屏幕截图和示例
分析结果
*Notion定价页面分析截图*
CLI输出示例
$ competitor-hunter https://www.notion.so/pricing
🔍 正在分析: https://www.notion.so/pricing
✅ 分析完成!
======================================================================
📦 产品名称: Notion
🔗 URL: https://www.notion.so/pricing
🕒 更新时间: 2024-06-13 00:00:00+00:00
======================================================================
💰 定价方案 (4 个):
• Free: 0 USD / monthly
• Plus: 10 USD / monthly
• Business: 20 USD / monthly
• Enterprise: Custom USD / custom
✨ 核心功能 (13 个):
1. AI automation
2. Enterprise search
3. Meeting notes
...
💾 结果已保存到: reports/product_Notion.jsonJSON输出结构
分析结果保存为结构化JSON文件:
{
"product_name": "Notion",
"url": "https://www.notion.so/pricing",
"pricing_tiers": [
{
"name": "Free",
"price": "0",
"currency": "USD",
"billing_cycle": "monthly"
},
{
"name": "Plus",
"price": "10",
"currency": "USD",
"billing_cycle": "monthly"
}
],
"core_features": [
"AI automation",
"Docs",
"Knowledge Base"
],
"summary": "## 产品概述\nNotion 是一款集文档编辑...",
"last_updated": "2024-06-13T00:00:00Z"
}______________________________________________________________________
📚 用法
方法1:CLI命令(最简单)
安装后,使用 competitor-hunter 命令:
# Analyze a single website
competitor-hunter https://www.notion.so/pricing
# Specify output file
competitor-hunter https://example.com output.json
# Batch analysis
competitor-hunter https://site1.com https://site2.com https://site3.com结果会自动保存到 reports/ 使用正确的UTF-8编码的目录。
方法2:MCP服务器模式(推荐给AI助手)
运行MCP服务器以启用与Claude Desktop或Cursor的集成:
python -m src.competitor_hunter.interface.mcp_server.serverClaude桌面集成
将以下配置添加到您的Claude桌面 claude_desktop_config.json:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json\ 视窗: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"competitor-hunter": {
"command": "python",
"args": [
"-m",
"src.competitor_hunter.interface.mcp_server.server"
],
"cwd": "/path/to/competitor-hunter"
}
}
}游标IDE集成
创建 .cursor/mcp.json 在项目根目录中:
{
"mcpServers": {
"competitor-hunter": {
"command": "python",
"args": [
"-m",
"src.competitor_hunter.interface.mcp_server.server"
],
"cwd": "${workspaceFolder}"
}
}
}重启后,您可以直接在聊天中使用该工具:
Analyze this competitor: https://www.notion.so/pricing方法3:Python库
直接在Python代码中使用LangGraph工作流:
import asyncio
from competitor_hunter.core import graph, AgentState, cleanup_resources
async def analyze(url: str):
# Initialize state
initial_state: AgentState = {
"url": url,
"scraped_content": None,
"product": None,
"error": None,
}
# Run workflow
result = await graph.ainvoke(initial_state)
# Check results
if result.get("error"):
print(f"Error: {result['error']}")
return None
product = result["product"]
print(f"Product: {product.product_name}")
print(f"Pricing Tiers: {len(product.pricing_tiers)}")
print(f"Features: {product.core_features}")
return product
# Use
product = await analyze("https://www.notion.so/pricing")
await cleanup_resources()输出结构
所有分析结果都保存到 reports/ 目录:
reports/
├── product_Notion.json
├── product_Example_Domain.json
└── ...每个JSON文件包含:
- 产品名称和URL
- 定价层次(名称、价格、货币、计费周期)
- 核心功能列表
- 带SWOT分析的Markdown格式摘要
- 上次更新时间戳
______________________________________________________________________
🧪 发展
运行测试
# Run all tests
pytest tests/ -v
# Run specific test file
pytest tests/test_crawler.py -v
# Run with coverage
pytest tests/ --cov=src/competitor_hunter --cov-report=html代码质量
# Format code
black src/ tests/
# Lint code
ruff check src/ tests/
# Type checking (if using mypy)
mypy src/项目结构
competitor-hunter/
├── src/
│ └── competitor_hunter/
│ ├── cli.py # CLI command-line interface
│ ├── main.py # Application entry point
│ ├── config.py # Configuration management
│ ├── core/ # Domain models & LangGraph workflow
│ │ ├── models.py # Pydantic models (CompetitorProduct, etc.)
│ │ └── graph.py # LangGraph workflow definition
│ ├── infrastructure/ # External services
│ │ ├── browser/ # Playwright browser service
│ │ └── llm/ # LLM extractor service
│ └── interface/ # Entry points
│ └── mcp_server/ # MCP server implementation
├── config/ # Configuration files
│ └── app.yaml.example # Configuration template
├── docker/ # Docker configuration
│ ├── Dockerfile # Docker image definition
│ └── docker-compose.yml # Docker Compose configuration
├── examples/ # Example scripts
├── tests/ # Test suite
├── reports/ # Analysis results (gitignored)
├── data/ # SQLite database (gitignored)
├── logs/ # Screenshots & logs (gitignored)
├── pyproject.toml # Project dependencies & CLI entry points
└── README.md # This file______________________________________________________________________
📦 依赖项
核心依赖关系
- 主控程序:模型上下文协议服务器实现
- 兰格拉夫:工作流编排
- 语言链:LLM集成框架
- 剧作家:浏览器自动化
- 皮丹提克:数据验证和序列化
- html2text:HTML到Markdown的转换
- 日志库:结构化日志记录
发展依赖性
- pytest:测试框架
- pytest异步:异步测试支持
- 颈毛:快速Python linter
- 黑色:代码格式化程序
______________________________________________________________________
🤝 贡献
欢迎投稿!请随时提交拉取请求。
- 克隆该仓库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add some amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
______________________________________________________________________
📄 许可证
该项目根据 MIT许可证 -看看 许可证 文件以获取详细信息。
______________________________________________________________________
🙏 致谢
- 建于 LangGraph 用于工作流编排
- 由...驱动 剧作家 用于浏览器自动化
- 与...集成 模型上下文协议(MCP) 用于AI代理通信
______________________________________________________________________
📞 支持
对于问题、疑问或贡献,请在 .
______________________________________________________________________
由...制作❤️ 竞争情报
