网页抓取API

一个全面的网页抓取API,支持MCP服务器集成、SDK和Docker,可将任何网站转换为干净、结构化的数据。

  • by jotape4ai
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 jotape4ai/web-scraper-api

使用说明

🔥 Web Scraper API

全面的web抓取API,支持MCP服务器集成、SDK和Docker。将任何网站转换为干净、结构化的数据。

🌟 特性

  • 🕷️ 个人报废:从特定URL提取内容
  • 🔄 完全爬行:抓取网站的所有可访问页面
  • 📝 多种格式:Markdown、HTML、截图
  • ⚡ 异步:带有Redis的队列系统用于后台处理
  • 🛡️ 健壮:错误处理、速率限制和验证
  • 🎯 可定制的:灵活的抓取和过滤选项

🚀 快速开始

先决条件

  • Node.js 18+
  • 雷迪斯
  • npm或pnpm

安装

  1. 克隆和设置:
cd web-scraper-api
npm run install:all
  1. 配置环境变量:
cp api/env.example api/.env
# Edit api/.env as needed
  1. 启动Redis:
redis-server
  1. 启动服务器:
npm run start:dev

API将于 http://localhost:3002

📋 API终点

系统健康

# Check that the API is working
curl http://localhost:3002/health

个人报废

# Basic scrape
curl -X POST http://localhost:3002/api/scrape \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com"
  }'

# Scrape with options
curl -X POST http://localhost:3002/api/scrape \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com",
    "options": {
      "includeHtml": true,
      "includeMarkdown": true,
      "includeScreenshot": false,
      "waitFor": 2000,
      "onlyMainContent": true
    }
  }'

批量报废

curl -X POST http://localhost:3002/api/scrape/batch \
  -H "Content-Type: application/json" \
  -d '{
    "urls": [
      "https://example.com",
      "https://example.com/about",
      "https://example.com/contact"
    ],
    "options": {
      "includeMarkdown": true
    }
  }'

爬行

# Start crawl
curl -X POST http://localhost:3002/api/crawl \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com",
    "options": {
      "maxPages": 10,
      "maxDepth": 2,
      "includeSubdomains": false
    }
  }'

# Check crawl status
curl http://localhost:3002/api/crawl/{job-id}

⚙️ 配置选项

报废选项

选项 类型 默认值 描述
includeHtml boolean false 包含原始HTML
includeMarkdown boolean true 在Markdown中包含内容
includeScreenshot boolean false 包括屏幕截图
waitFor number 0 等待时间(毫秒)
timeout number 30000 请求超时
userAgent string - 自定义用户代理
headers object - 自定义HTTP标头
excludeSelectors string[] - 要排除的CSS选择器
onlyMainContent boolean false 只有主要内容

爬行选项

包括所有抓取选项以及:

选项 类型 默认值 描述
maxPages number 10 可抓取的最大页面数
maxDepth number 3 最大深度
allowedDomains string[] - 允许的域
excludePatterns string[] - 要排除的URL模式
includeSubdomains boolean false 包含子域
respectRobotsTxt boolean false 尊重robots.txt

🏗️ 建筑

web-scraper-api/
├── api/                    # API Server
│   ├── src/
│   │   ├── routes/        # HTTP routes
│   │   ├── services/      # Business logic
│   │   ├── types/         # Type definitions
│   │   ├── utils/         # Utilities
│   │   └── workers/       # Queue workers
│   └── package.json
├── sdk/                   # TypeScript SDK
│   ├── src/               # API client
│   └── package.json
├── mcp-server/            # MCP Server for Cursor
│   ├── src/               # MCP server
│   └── package.json
├── examples/              # Usage examples
├── docs/                  # Documentation
├── install-mcp.sh         # MCP installation script
└── start-mcp-server.sh    # MCP startup script

🛠️ 发展

可用脚本

# Install all dependencies
npm run install:all

# Development (with hot reload)
npm run start:dev

# Production
npm run build
npm start

# Tests
npm test

# Start workers
cd api && npm run workers

开发配置

这 api/.env 文件应包括:

PORT=3002
HOST=0.0.0.0
REDIS_URL=redis://localhost:6379
NUM_WORKERS=4
PUPPETEER_HEADLESS=true
LOG_LEVEL=info

🤖 MCP游标服务器

现在,您可以直接在Cursor中使用Web Scraper API和我们的MCP服务器!

快速安装

# Install everything automatically
./install-mcp.sh

# Start server
./start-mcp-server.sh

光标配置

将此添加到光标 settings.json:

{
  "mcp.servers": {
    "web-scraper-api": {
      "command": "node",
      "args": ["/full/path/to/web-scraper-api/mcp-server/dist/index.js"],
      "env": {
        "WEB_SCRAPER_API_URL": "http://localhost:3002"
      }
    }
  }
}

游标中的用法

配置后,您可以使用以下命令:

  • “从中提取内容https://example.com"
  • “批量删除这些URL:[URLs]”
  • “抓取整个网站https://blog.example.com"

完整的文件: mcp服务器/README.md

🚀 部署

使用Docker

# Coming soon - Docker Compose
docker-compose up

手册

  1. 在生产环境中配置Redis
  2. 设置环境变量
  3. 构建项目: npm run build
  4. 启动服务器: npm start
  5. 启动工人: npm run workers

🔧 技术

  • 后端:Node.js、TypeScript、Express
  • Web剪贴:木偶,切里奥
  • 队列:BullMQ+Redis
  • 处理:关闭服务(HTML→ Markdown)
  • 安全:头盔、CORS、速率限制

📝 路线图

  • []JavaScript/TypeScript SDK
  • []Python SDK
  • []Web管理界面
  • []身份验证支持
  • [ ] 网钩通知
  • []Docker容器
  • []指标和监测
  • []智能缓存
  • []代理支持

🤝 贡献

  1. 分叉项目
  2. 创建要素分支(git checkout -b feature/new-feature)
  3. 提交更改(git commit -am 'Add new feature')
  4. 推到分支(git push origin feature/new-feature)
  5. 创建拉取请求

📄 许可证

MIT许可证-请参阅 许可证 了解更多详情。

🙏 灵感

这个项目的灵感来自 萤火虫 并作为一个简化的概念证明,以理解大规模网络抓取的概念。