OpenClaw Web 工具:网络搜索与内容抓取

OpenClaw 提供了强大的 Web 工具,让你的 AI 助手能够搜索网络和抓取网页内容。本文将详细介绍如何配置和使用 Web 搜索和 Web 获取功能。

Web 工具概述

OpenClaw 提供两个主要的 Web 工具:

  • web_search:使用搜索引擎查询网络信息
  • web_fetch:获取指定 URL 的内容并提取可读文本

Web 搜索(web_search)

web_search 工具允许 AI Agent 搜索网络,获取最新信息。

支持的搜索引擎

搜索引擎 提供商 特点
Perplexity Perplexity AI AI 驱动,高质量结果
Brave Search Brave 隐私保护
Gemini Google 整合 Google 搜索
Grok xAI 实时信息
Kimi 月之暗面 中文优化

配置 Web 搜索

使用配置向导:

$ openclaw configure --section web

或手动配置:

{
  "tools": {
    "web": {
      "search": {
        "enabled": true,
        "provider": "perplexity",
        "apiKey": "YOUR_PERPLEXITY_API_KEY",
        "maxResults": 5,
        "cache": {
          "enabled": true,
          "ttl": 900
        }
      }
    }
  }
}

Perplexity 搜索配置

Perplexity 是推荐的搜索提供商,需要 API Key:

{
  "tools": {
    "web": {
      "search": {
        "enabled": true,
        "provider": "perplexity",
        "apiKey": "pplx-xxxxxxxxxxxxxxxxxxxxxxxx",
        "model": "llama-3.1-sonar-small-online",
        "maxResults": 5
      }
    }
  }
}

Brave Search 配置

Brave Search 提供隐私保护的搜索:

{
  "tools": {
    "web": {
      "search": {
        "enabled": true,
        "provider": "brave",
        "apiKey": "YOUR_BRAVE_API_KEY",
        "maxResults": 10
      }
    }
  }
}

搜索参数

参数 说明 默认值
query 搜索查询 必需
count 结果数量(1-10) 5
provider 搜索提供商 全局配置
language 搜索语言 auto

Web 获取(web_fetch)

web_fetch 工具获取指定 URL 的内容,并提取可读文本。

配置 Web 获取

{
  "tools": {
    "web": {
      "fetch": {
        "enabled": true,
        "timeout": 10000,
        "maxSize": 1048576,
        "cache": {
          "enabled": true,
          "ttl": 3600
        }
      }
    }
  }
}

获取参数

参数 说明 默认值
url 目标 URL 必需
extract 提取可读文本 true
format 输出格式(markdown, text, html) markdown

使用 Web 工具

通过对话使用

直接在聊天中使用:

用户: 搜索最新的 AI 技术趋势
AI: [使用 web_search 工具]
AI: 根据搜索结果,最新的 AI 技术趋势包括...
用户: 总结 https://example.com/article 的内容
AI: [使用 web_fetch 工具]
AI: 该文章的主要内容是...

通过工具调用

直接调用工具:

@openclaw 搜索 "Python 编程教程"
@openclaw 获取 https://example.com 的内容

高级功能

搜索结果过滤

过滤特定网站或域名:

用户: 搜索 Python 教程,只看 Python.org 官方网站
AI: [搜索时添加 site:python.org 过滤]

多语言搜索

指定搜索语言:

用户: 用中文搜索 Python 教程
AI: [搜索时指定 language=zh-CN]

搜索历史记录

查看搜索历史:

$ openclaw web search history

缓存机制

Web 工具支持缓存,减少重复请求:

配置缓存

{
  "tools": {
    "web": {
      "search": {
        "cache": {
          "enabled": true,
          "ttl": 900,
          "maxSize": 1000
        }
      },
      "fetch": {
        "cache": {
          "enabled": true,
          "ttl": 3600
        }
      }
    }
  }
}

清除缓存

# 清除所有缓存
$ openclaw web cache clear

# 清除特定缓存
$ openclaw web cache clear --type search

实际应用场景

场景 1:新闻摘要

定时搜索新闻并生成摘要:

用户: 总结今天的科技新闻
AI: [搜索 "科技新闻",获取多个来源,生成摘要]
AI: 今天的科技新闻要点:
1. OpenAI 发布新模型...
2. Google 宣布新功能...

场景 2:价格监控

定期检查商品价格:

用户: 检查 iPhone 15 在 Amazon 的价格
AI: [获取 Amazon 产品页面,提取价格]
AI: iPhone 15 当前价格为 $799

场景 3:知识查询

搜索特定知识:

用户: 2024 年诺贝尔奖得主是谁?
AI: [搜索 "2024 Nobel Prize winners"]
AI: 2024 年诺贝尔奖得主:
- 物理学奖:...
- 化学奖:...

场景 4:文档分析

获取并分析在线文档:

用户: 总结 https://docs.example.com/guide 的 API
AI: [获取文档,分析 API 结构]
AI: 该文档包含以下 API:
- GET /users - 获取用户列表
- POST /users - 创建用户...

性能优化

减少延迟

  • 启用缓存
  • 选择快速的搜索引擎
  • 限制结果数量

降低成本

  • 使用免费搜索引擎(Brave)
  • 启用缓存减少 API 调用
  • 限制搜索频率

故障排查

搜索失败

  • 检查 API Key 是否正确
  • 确认网络连接正常
  • 查看网关日志

获取失败

  • 检查 URL 是否有效
  • 确认网站允许抓取
  • 检查超时设置

最佳实践

合理使用

  • 避免过于频繁的搜索
  • 使用缓存减少重复请求
  • 限制结果数量

合规使用

  • 遵守网站 robots.txt
  • 尊重版权和隐私
  • 引用来源

总结

Web 工具让 OpenClaw 能够访问实时网络信息,大大扩展了 AI 助手的能力。通过合理配置和优化,你可以让 AI 助手高效地搜索和获取网络内容。

相关资源

发表回复

后才能评论