网页爬取工具

Crawl4AI MCP Server是一个生产就绪的网页爬取和内容提取工具,专为AI优化,提供多种内容提取方式,适用于数据分析、内容归档和网页开发等场景。

  • by azure-architect
  • HTML
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 azure-architect/crawl4ai-mcp-server

使用说明

Crawl4AI MCP服务器

Crawl4AI(开源、LLM友好的网络爬虫)的生产就绪模型上下文协议(MCP)服务器集成。该项目直接在Claude Desktop和Claude Code中提供对高级网络爬行和内容提取功能的无缝访问。

🚀 快速开始

# Pull and run Crawl4AI
docker run -d -p 11235:11235 --name crawl4ai --shm-size=1g unclecode/crawl4ai:latest

# Add to Claude Desktop configuration
{
  "mcpServers": {
    "c4ai-sse": {
      "command": "sse",
      "args": ["http://localhost:11235/mcp/sse"]
    }
  }
}

就是这样! 您现在可以在Claude中访问7个强大的网络爬行工具。

🛠 可用工具

工具 描述 最适合
md 从网页中提取干净的标记 博客帖子、文章、文档
html 提取原始HTML内容 Web开发、DOM分析
screenshot 捕获高分辨率屏幕截图 视觉验证、存档
pdf 从网页生成PDF 文档存档,离线阅读
execute_js 在页面上执行JavaScript 动态内容,自定义提取
crawl 批量处理多个URL 大量抓取、比较
ask 查询Crawl4AI的内置知识 库文档,API参考

🎯 主要特点

  • 生产就绪:基于Docker的部署,具有健康检查和监控功能
  • LLM优化:专门为AI消费设计的内容提取
  • 高性能:使用智能缓存进行并发处理
  • 全面的:截图、PDF生成、JavaScript执行
  • 可靠的:强大的错误处理和超时管理
  • 可扩展的:Kubernetes和云平台支持

📊 业绩亮点

通过7种工具的全面测试:

  • 成功率:100%覆盖所有核心功能
  • 响应时间:每个请求平均2-8秒
  • 内容质量:始终干净、格式良好的输出
  • 可靠性:对复杂网站和边缘案例的稳健处理
  • 并发:高效批处理多个URL

📚 文档

核心文件

快速链接

🔧 系统要求

  • 码头工人:版本20.10+
  • 记忆:最低2GB RAM(建议4GB)
  • 存储:2GB可用磁盘空间
  • 网络:用于图像提取的互联网连接
  • 克劳德:支持MCP的桌面或代码

📦 安装方法

方法一:Docker(推荐)

# Basic installation
docker run -d \
  -p 11235:11235 \
  --name crawl4ai \
  --shm-size=1g \
  unclecode/crawl4ai:latest

方法二:Docker编写

version: '3.8'
services:
  crawl4ai:
    image: unclecode/crawl4ai:latest
    container_name: crawl4ai
    ports:
      - "11235:11235"
    shm_size: '1gb'
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:11235/"]
      interval: 30s
      timeout: 10s
      retries: 3

方法3:Kubernetes

apiVersion: apps/v1
kind: Deployment
metadata:
  name: crawl4ai
spec:
  replicas: 1
  selector:
    matchLabels:
      app: crawl4ai
  template:
    metadata:
      labels:
        app: crawl4ai
    spec:
      containers:
      - name: crawl4ai
        image: unclecode/crawl4ai:latest
        ports:
        - containerPort: 11235
        resources:
          requests:
            memory: "1Gi"
            cpu: "500m"
          limits:
            memory: "2Gi"
            cpu: "1"

🎨 基本用法

摘录文章内容

curl -X POST http://localhost:11235/md \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://en.wikipedia.org/wiki/Web_scraping",
    "f": "fit"
  }'

捕获屏幕截图

curl -X POST http://localhost:11235/screenshot \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://github.com",
    "screenshot_wait_for": 3
  }'

执行JavaScript

curl -X POST http://localhost:11235/execute_js \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://news.ycombinator.com",
    "scripts": [
      "Array.from(document.querySelectorAll(\".titleline a\")).slice(0, 5).map(a => ({title: a.textContent, url: a.href}))"
    ]
  }'

批处理

curl -X POST http://localhost:11235/crawl \
  -H "Content-Type: application/json" \
  -d '{
    "urls": [
      "https://example.com",
      "https://httpbin.org/html",
      "https://jsonplaceholder.typicode.com"
    ]
  }'

🚀 高级功能

自定义浏览器配置

{
  "url": "https://example.com",
  "browser_config": {
    "viewport_width": 1920,
    "viewport_height": 1080,
    "user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36",
    "headless": true
  }
}

内容过滤选项

{
  "url": "https://example.com",
  "f": "fit",        // "raw", "fit", "bm25", "llm"
  "q": null,         // Query for bm25/llm filtering
  "c": "0"           // Content filtering level
}

JavaScript执行模式

// Extract structured data
{
  title: document.title,
  headings: Array.from(document.querySelectorAll('h1,h2,h3')).map(h => h.textContent),
  links: Array.from(document.querySelectorAll('a')).slice(0, 10).map(a => ({
    text: a.textContent.trim(),
    url: a.href
  }))
}

🏭 生产部署

Docker Swarm的高可用性

docker service create \
  --name crawl4ai \
  --replicas 3 \
  --publish 11235:11235 \
  --mount type=volume,source=crawl4ai-cache,target=/app/cache \
  unclecode/crawl4ai:latest

云平台部署

AWS ECS, 谷歌云运行, Azure容器实例 -看 部署指导 获取完整说明。

监测和可观察性

# docker-compose.monitoring.yml
services:
  crawl4ai:
    image: unclecode/crawl4ai:latest
    environment:
      - METRICS_ENABLED=true
      - LOG_LEVEL=INFO
  
  prometheus:
    image: prom/prometheus
    ports:
      - "9090:9090"

🔍 健康检查和监测

# Basic health check
curl http://localhost:11235/

# Schema validation
curl http://localhost:11235/mcp/schema | jq '.tools | length'

# Performance monitoring
docker stats crawl4ai

🛡 安全功能

  • 网络隔离 使用自定义Docker网络
  • 资源限制 防止资源枯竭
  • 只读容器 增强安全性
  • 非根执行 具有适当的用户权限

📈 性能调整

内存优化

docker run -d \
  --memory=4g \
  --memory-swap=4g \
  --shm-size=2g \
  --name crawl4ai \
  unclecode/crawl4ai:latest

CPU优化

docker run -d \
  --cpus="2.0" \
  --cpuset-cpus="0,1" \
  --name crawl4ai \
  unclecode/crawl4ai:latest

浏览器性能

{
  "browser_config": {
    "text_mode": true,
    "light_mode": true,
    "extra_args": [
      "--disable-images",
      "--disable-javascript",
      "--disable-plugins"
    ]
  }
}

🧪 测试与验证

自动化测试套件

# Run comprehensive tests
./test-crawl4ai.sh

# Expected results:
# 1. Health Check: ✓ PASS
# 2. Schema Check: ✓ PASS  
# 3. MD Tool Check: ✓ PASS
# 4. Screenshot Tool Check: ✓ PASS
# 5. JavaScript Tool Check: ✓ PASS

手动验证

# Test each tool individually
curl -X POST http://localhost:11235/md -H "Content-Type: application/json" -d '{"url": "https://example.com", "f": "fit"}'
curl -X POST http://localhost:11235/screenshot -H "Content-Type: application/json" -d '{"url": "https://example.com"}'
curl -X POST http://localhost:11235/execute_js -H "Content-Type: application/json" -d '{"url": "https://example.com", "scripts": ["document.title"]}'

🔧 配置

环境变量

# Logging
LOG_LEVEL=INFO              # DEBUG, INFO, WARNING, ERROR
MAX_WORKERS=4               # Maximum concurrent workers
TIMEOUT=30                  # Default request timeout

# Features  
CACHE_ENABLED=true          # Enable caching
SCREENSHOT_ENABLED=true     # Enable screenshots
PDF_ENABLED=true            # Enable PDF generation

# Security
RATE_LIMIT=100              # Requests per minute
MAX_URL_LENGTH=2048         # Maximum URL length

Claude桌面集成

{
  "mcpServers": {
    "c4ai-sse": {
      "command": "sse",
      "args": ["http://localhost:11235/mcp/sse"],
      "env": {
        "LOG_LEVEL": "INFO",
        "TIMEOUT": "30"
      }
    }
  }
}

📊 用例

内容分析管道

def comprehensive_analysis(url):
    # Step 1: Extract content
    md_result = call_tool("md", {"url": url, "f": "fit"})
    
    # Step 2: Analyze structure  
    js_result = call_tool("execute_js", {
        "url": url,
        "scripts": ["({wordCount: document.body.textContent.split(' ').length})"]
    })
    
    # Step 3: Visual verification
    screenshot_result = call_tool("screenshot", {"url": url})
    
    return combine_results(md_result, js_result, screenshot_result)

竞争监控

def monitor_competitors(urls):
    # Batch process all competitors
    batch_result = call_tool("crawl", {"urls": urls})
    
    # Extract pricing and product info
    for result in batch_result["results"]:
        if result["success"]:
            analyze_competitor_data(result)

🚨 故障排除

常见问题

问题 解决方案
端口已在使用中 使用 -p 8080:11235 对于备选端口
容器无法启动 检查 docker logs crawl4ai 对于错误
MCP连接失败 使用验证端点 curl http://localhost:11235/mcp/sse
内存使用率高 添加 --memory=4g --shm-size=2g 限制
请求超时 增加刀具参数的超时时间

诊断命令

# Container status
docker ps | grep crawl4ai
docker inspect crawl4ai --format='{{.State.Health.Status}}'

# Network connectivity
curl -I http://localhost:11235/
telnet localhost 11235

# Resource usage
docker stats crawl4ai

🤝 贡献

这是一个开源Crawl4AI库的部署和集成项目。对于核心Crawl4AI开发:

对于MCP集成问题或部署改进,请在此存储库中打开问题。

📄 许可证

此项目遵循底层Crawl4AI项目的许可。有关许可证信息,请参阅官方Crawl4AI存储库。

🙏 致谢

  • Crawl4AI团队 -创建优秀的开源网络爬虫库
  • Anthropic -用于模型上下文协议规范和Claude集成
  • 社区贡献者 -用于测试、反馈和改进

📞 支持

  • 文档:综合指南 /docs 目录
  • 问题:通过GitHub Issues报告问题
  • 社区:加入Crawl4AI社区的讨论
  • 商业的:联系以获取企业支持和自定义部署

🎉 准备好开始爬行了吗? 跟随 快速开始 在上面引导或潜入 安装与设置指南 详细说明。


最后更新日期:2025年8月4日
版本:1.0.0