Anyhunt

Crawl API

爬取网站多个页面,支持深度和路径控制

Crawl API

Crawl API 支持多页面网站爬取,包括深度控制、路径过滤和 Webhook 通知。适用于抓取整个网站或特定部分。

接口端点

方法路径描述
POST/api/v1/crawl创建爬取任务
GET/api/v1/crawl/:id获取爬取状态和结果
DELETE/api/v1/crawl/:id取消爬取任务
GET/api/v1/crawl获取爬取历史

创建爬取任务

POST /api/v1/crawl

请求参数

参数类型默认值描述
urlstring必填爬取起始 URL
maxDepthnumber3最大链接深度(1-10)
limitnumber100最大爬取页面数(1-1000)
includePathsstring[]-要包含的 URL 模式(glob 模式)
excludePathsstring[]-要排除的 URL 模式(glob 模式)
allowExternalLinksbooleanfalse是否跟踪外部域名链接
scrapeOptionsobject-每个页面的抓取选项(参见 Scrape API)
webhookUrlstring-完成时的 Webhook 通知 URL

请求示例

curl -X POST https://server.anyhunt.app/api/v1/crawl \
  -H "Authorization: Bearer ah_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://docs.example.com",
    "maxDepth": 3,
    "limit": 50,
    "includePaths": ["/docs/*", "/guides/*"],
    "excludePaths": ["/api/*"],
    "scrapeOptions": {
      "formats": ["markdown"],
      "onlyMainContent": true
    },
    "webhookUrl": "https://your-app.com/webhooks/crawl"
  }'

响应

API 返回任务 ID。通过轮询 GET /api/v1/crawl/:id 获取状态和结果。

{
  "id": "crawl_abc123",
  "status": "PENDING"
}

获取爬取状态

GET /api/v1/crawl/:id

获取爬取任务的状态和结果。

响应(进行中)

{
  "id": "crawl_abc123",
  "status": "PROCESSING",
  "startUrl": "https://docs.example.com",
  "totalUrls": 45,
  "completedUrls": 32,
  "failedUrls": 2,
  "createdAt": "2024-01-15T10:30:00.000Z",
  "startedAt": "2024-01-15T10:30:01.000Z"
}

响应(已完成)

{
  "id": "crawl_abc123",
  "status": "COMPLETED",
  "startUrl": "https://docs.example.com",
  "totalUrls": 45,
  "completedUrls": 43,
  "failedUrls": 2,
  "createdAt": "2024-01-15T10:30:00.000Z",
  "startedAt": "2024-01-15T10:30:01.000Z",
  "completedAt": "2024-01-15T10:32:15.000Z",
  "data": [
    {
      "url": "https://docs.example.com/intro",
      "depth": 1,
      "markdown": "# 简介\n\n欢迎使用...",
      "metadata": {
        "title": "简介",
        "description": "入门指南"
      },
      "links": ["https://docs.example.com/setup", "..."]
    }
  ]
}

状态值:

状态描述
PENDING任务已排队
PROCESSING爬取进行中
COMPLETED爬取成功完成
FAILED爬取失败
CANCELLED爬取已取消

取消爬取任务

DELETE /api/v1/crawl/:id

取消正在运行的爬取任务。

响应

{
  "id": "crawl_abc123",
  "status": "CANCELLED"
}

获取爬取历史

GET /api/v1/crawl

查询参数

参数类型默认值描述
limitnumber20最大结果数(1-100)
offsetnumber0跳过的结果数,用于分页

响应

[
  {
    "id": "crawl_abc123",
    "status": "COMPLETED",
    "startUrl": "https://docs.example.com",
    "totalUrls": 45,
    "completedUrls": 43,
    "failedUrls": 2,
    "createdAt": "2024-01-15T10:30:00.000Z"
  }
]

Webhook 负载

爬取任务完成时,Webhook 接收:

{
  "event": "crawl.completed",
  "data": {
    "id": "crawl_abc123",
    "status": "COMPLETED",
    "startUrl": "https://docs.example.com",
    "totalUrls": 45,
    "completedUrls": 43,
    "failedUrls": 2
  },
  "timestamp": "2024-01-15T10:32:15.000Z"
}

路径过滤

includePathsexcludePaths 使用 glob 模式:

模式匹配
/docs/*/docs/intro/docs/guide
/docs/**/docs/intro/docs/api/reference
*.pdf任何 PDF 文件
/blog/2024-*/blog/2024-01-post/blog/2024-02-news

最佳实践

  1. 从小规模开始 - 先用较低的 limit(10-20)测试,再爬取整个网站
  2. 使用路径过滤 - 通过 includePaths 聚焦相关内容
  3. 设置 Webhook - 对于 limit > 50 的爬取,使用 Webhook 而非轮询
  4. 注意速率限制 - 大规模爬取消耗更多配额,可能需要更长时间