OpenClaw 数据提取 Agent 搭建

让AI帮你从网页中提取数据 — 结构化、自动化、批量处理

凌晨4点05分,我在Hacker News上看到157个帖子,人工看要看到天亮。但我的数据提取Agent只用了12秒——它不仅读完了所有标题,还提取了发布时间、点赞数、评论数,按热度排了个序,顺便把有价值的链接存进了数据库。这就是数据提取Agent的价值:把人从重复劳动中解放出来。

数据提取Agent能力矩阵

基础数据提取

1. 单页面提取

# 使用 web_fetch 抓取静态页面
web_fetch(
  url="https://news.ycombinator.com/",
  extractMode="markdown",
  maxChars=5000
)

# 使用 browser 工具处理动态页面
browser(action="open", targetUrl="https://example.com")
browser(action="snapshot", snapshotFormat="aria")
browser(action="close")

2. 结构化提取示例

# 从Hacker News提取热门帖子
# 步骤1: 抓取页面
web_fetch(url="https://news.ycombinator.com/", maxChars=10000)

# 步骤2: LLM提取结构化数据
# Prompt示例:
"""
从以下HTML内容中提取热门帖子信息,输出JSON数组:
- title: 标题
- url: 链接
- points: 点赞数
- comments: 评论数
- source: 来源域名

要求:
1. 只提取前20条
2. 按点赞数降序排列
3. 过滤掉没有URL的帖子
"""

# 步骤3: 输出结果
[
  {
    "title": "DeepSeek V4",
    "url": "https://api-docs.deepseek.com/",
    "points": 1551,
    "comments": 1183,
    "source": "deepseek.com"
  },
  ...
]

批量数据采集

翻页采集

# 批量采集多页数据
# 适用于分页列表页面

# 方法一:计算URL模式
# https://example.com/products?page=1
# https://example.com/products?page=2
# ...

# Agent自动执行
for page in range(1, 11):
    url = f"https://example.com/products?page={page}"
    data = web_fetch(url, maxChars=5000)
    items = extract_structured_data(data)
    save_to_database(items)
    sleep(random_delay())  # 礼貌爬取

多站点采集

# 从多个竞品网站采集数据
sources = [
    {"name": "futuretools", "url": "https://futuretools.io"},
    {"name": "theresanaiforthat", "url": "https://theresanaiforthat.com"},
    {"name": "easywithai", "url": "https://easywithai.com"},
]

for source in sources:
    data = web_fetch(source["url"])
    tools = extract_ai_tools(data)  # 提取AI工具信息
    for tool in tools:
        tool["source_site"] = source["name"]
    save_to_database(tools)

数据提取到飞书表格

# 将提取的数据写入飞书多维表格
# 适用于团队协作场景

# 步骤1: 获取表格信息
feishu_bitable_get_meta(url="飞书表格URL")

# 步骤2: 创建记录
feishu_bitable_create_record(
  app_token="app_xxx",
  table_id="tblxxx",
  fields={
    "工具名称": "DeepSeek V4",
    "分类": "LLM",
    "评分": 4.8,
    "来源": "Hacker News",
    "采集时间": "2026-04-25 01:00",
    "状态": "待评估"
  }
)

反爬对抗策略

1. 礼貌爬取

# 基本礼貌规则
# 1. 随机延迟(2-5秒)
# 2. 遵守robots.txt
# 3. 设置合理的User-Agent
# 4. 限制并发请求数

OPENCLAW_SCRAPE_DELAY_MIN=2    # 最小延迟2秒
OPENCLAW_SCRAPE_DELAY_MAX=5    # 最大延迟5秒
OPENCLAW_SCRAPE_MAX_CONCURRENT=2  # 最大并发2
OPENCLAW_SCRAPE_RESPECT_ROBOTS=true

2. 浏览器模拟

# 使用browser工具模拟真实浏览器
# 适用于需要JS渲染的页面

# 启动浏览器
browser(action="start", profile="openclaw")

# 打开页面
browser(action="open", targetUrl="https://example.com")

# 等待页面加载
browser(action="snapshot", loadState="networkidle")

# 提取内容
snapshot = browser(action="snapshot", snapshotFormat="aria")

# 关闭浏览器
browser(action="close")

3. 错误处理与重试

# 遇到反爬时的处理策略
retry_strategy:
  max_retries: 3
  backoff: exponential
  base_delay: 5
  on_429:  # Rate Limited
    - wait_retry_after_header
    - increase_delay_multiplier: 2
  on_403:  # Forbidden
    - switch_user_agent
    - try_browser_mode
    - log_and_skip
  on_timeout:
    - retry_with_longer_timeout
    - log_warning

数据清洗与存储

数据清洗管道

# 提取后的数据清洗
cleaning_pipeline:
  # 1. 去重
  - deduplicate:
      key: ["title", "url"]
      strategy: "first_seen"
      
  # 2. 格式化
  - normalize:
      fields:
        - price: "float"        # 价格转数字
        - date: "iso8601"       # 日期标准化
        - url: "absolute"       # URL补全
        
  # 3. 验证
  - validate:
      rules:
        - "price > 0"
        - "title.length > 5"
        - "url matches ^https?://"
        
  # 4. 补全
  - enrich:
      - source_domain: "extract_from_url"
      - category: "auto_classify"

多格式存储

# 存储到不同格式

# CSV(数据分析用)
import csv
with open('tools.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=data[0].keys())
    writer.writeheader()
    writer.writerows(data)

# JSON(程序处理用)
import json
with open('tools.json', 'w') as f:
    json.dump(data, f, indent=2, ensure_ascii=False)

# HTML报告(人类阅读用)
# 生成带表格的HTML页面,部署到网站

# 数据库(长期存储用)
# INSERT INTO ai_tools (name, category, score, source) VALUES (...)

定时数据采集

# 设置定时采集任务

# 每日AI新闻采集(每天8点)
openclaw cron add \
  --name "daily-ai-news" \
  --schedule "0 8 * * *" \
  --task "采集AI新闻并生成日报"

# 每周竞品监控(每周一9点)
openclaw cron add \
  --name "weekly-competitor" \
  --schedule "0 9 * * 1" \
  --task "采集竞品网站更新数据"

# 每小时热点追踪(工作时间每小时)
openclaw cron add \
  --name "hourly-trending" \
  --schedule "0 9-22 * * 1-5" \
  --task "采集Hacker News和Reddit热门AI话题"
⚠️ 合规提醒:数据采集需遵守目标网站的robots.txt、Terms of Service和《数据安全法》《个人信息保护法》。只采集公开数据,不采集个人信息。批量采集前务必设置合理延迟,避免对目标服务器造成压力。

常见问题

Q: web_fetch 和 browser 工具怎么选?

web_fetch 适合静态页面(速度快、成本低),browser 适合需要JS渲染的动态页面(速度慢、资源消耗大)。建议先用web_fetch尝试,获取失败或内容不完整时再用browser。

Q: LLM提取数据准确吗?

对于结构清晰的页面(表格、列表),LLM提取准确率可达95%+。对于非结构化内容,建议结合CSS选择器或XPath精确定位。重要数据建议人工抽检10%校准。

Q: 大规模采集会被封IP吗?

会,如果不注意的话。预防措施:1) 控制请求频率(2-5秒间隔)2) 使用代理IP轮换 3) 尊重robots.txt 4) 设置合理的User-Agent。商业化采集建议使用专业代理服务。

相关教程