网页数据提取平台

一个生产级的通用网页数据提取平台,支持静态和动态内容抓取,具备智能抓取策略自动检测、高级反机器人机制和内置前端仪表板。

  • by Barath2812
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Barath2812/mcpserver

使用说明

🌐 人工智能驱动的通用Web数据提取平台

一个生产级、支持MCP的通用网络抓取平台,由以下组件构建 快速API, MongoDB,以及 剧作家。它具有智能刮板自动检测、先进的反机器人机制和时尚的前端仪表板。


✨ 特性

  • 静态刮擦 --传统HTML页面的请求+BeautifulSoup
  • 动态刮擦 --JavaScript渲染页面的剧作家
  • 自动检测 --自动选择合适的刮刀策略
  • 反机器人保护 --用户代理轮换、速率限制、robots.txt合规性
  • MongoDB存储 --用丰富的元数据保存所有抓取的数据
  • MCP集成 --将抓取作为LLM调用的工具公开
  • 数据导出 --将抓取的数据导出为JSON或CSV
  • 前端仪表板 --内置web UI用于管理抓取

📁 项目结构

mcp/
├── main.py                 # FastAPI application entry point
├── config.py               # Configuration settings
├── mcp_server.py           # MCP server setup
├── mcp_config.json         # MCP configuration
├── requirements.txt        # Python dependencies
├── pyproject.toml          # Project metadata & build config
├── antigravity/            # Anti-bot & stealth modules
│   ├── stealth.py          # Stealth browsing utilities
│   ├── throttle.py         # Request throttling / rate limiting
│   ├── robots_validator.py # robots.txt compliance checker
│   └── user_agents.py      # User-Agent rotation
├── database/               # Database layer
│   ├── models.py           # Pydantic data models
│   └── mongodb.py          # MongoDB client & operations
├── scraper/                # Core scraping engines
│   ├── static_scraper.py   # BeautifulSoup-based scraper
│   ├── dynamic_scraper.py  # Playwright-based scraper
│   └── strategy_selector.py# Auto-detection logic
├── scraper_mcp/            # MCP tool wrappers
├── frontend/               # Web dashboard (HTML/CSS/JS)
│   ├── index.html
│   ├── styles.css
│   └── script.js
├── utils/                  # Utility modules
│   ├── exporter.py         # JSON/CSV export
│   └── normalizer.py       # Data normalization
├── tests/                  # Test suite
└── docs/                   # Documentation

🚀 入门指南

先决条件

  • python ≥ 3.10
  • MongoDB 在本地运行(默认值: mongodb://localhost:27017)

安装

  1. 克隆存储库

    git clone <repo-url>
    cd mcp
    
  2. 创建并激活虚拟环境

    python -m venv mcpvenv
    # Windows
    mcpvenv\Scripts\activate
    # macOS / Linux
    source mcpvenv/bin/activate
    
  3. 安装依赖项

    pip install -r requirements.txt
    
  4. 安装Playwright浏览器

    playwright install
    
  5. 设置环境变量 (可选)

    # Override defaults via .env or shell
    export MONGODB_URI="mongodb://localhost:27017"
    

运行服务器

python main.py

服务器启动于 http://localhost:8000。在浏览器中打开它以访问仪表板。


📡 API终点

方法 端点 描述
得到 / 前端仪表板
得到 /api API信息
职位 /scrape 抓取网站(JSON正文)
得到 /scrape 抓取网站(查询参数)
得到 /stats 报废统计
得到 /recent 最近抓取的数据
得到 /logs 报废原木
职位 /export/json 将数据导出为JSON
职位 /export/csv 将数据导出到CSV
得到 /health 健康检查
得到 /docs Swagger API文档

⚙️ 配置

所有配置都在中管理 config.py:

设置 默认值 说明
MONGODB_URI mongodb://localhost:27017 MongoDB连接字符串
SERVER_HOST 0.0.0.0 服务器绑定主机
SERVER_PORT 8000 服务器绑定端口
PLAYWRIGHT_HEADLESS True 在无头模式下运行浏览器
PLAYWRIGHT_TIMEOUT 30000 ms 页面加载超时
MAX_REQUESTS_PER_DOMAIN_PER_MINUTE 10 每个域的速率限制

🧪 运行测试

pytest

🛡️ 道德报废

该平台的设计考虑了道德刮擦:

  • ✅ 尊重 robots.txt 指令
  • ✅ 实现具有可配置延迟的礼貌爬行
  • ✅ 每个域的请求速率限制
  • ✅ 轮换用户代理以减少服务器负载模式

🛠️ 技术栈

  • 快速API --高性能异步web框架
  • 剧作家 --动态内容的浏览器自动化
  • 美丽汤 --静态内容的HTML解析
  • MongoDB+PyMongo --文档存储
  • MCP(模型上下文协议) --LLM工具集成
  • 派丹蒂克 --数据验证和序列化

📄 许可证

这个项目是供教育和个人使用的。