博查搜索 Python 版 (Bocha Search Python)
name: bocha-search-python
by andyli-gh · published 2026-04-01
$ claw add gh:andyli-gh/andyli-gh-bocha-search-python---
name: bocha-search-python
description: 博查搜索 (Bocha Search) 的 Python 实现技能,提供增强的网页搜索能力。当用户需要通过博查 AI 搜索 API 进行网页搜索、获取联网信息、查找最新资讯或中文内容时使用此技能。与现有的 JavaScript 版本相比,本技能提供更稳定的连接、更灵活的输出格式(原始 JSON/Brave 兼容格式/Markdown)、更好的错误处理和重试机制。适用于 AI Agent 需要联网搜索、RAG 应用获取网页摘要、中文内容检索等场景。
---
# 博查搜索 Python 版 (Bocha Search Python)
概述
博查搜索 Python 版是一个增强的博查 AI 搜索 API 客户端,专为 AI Agent 和自动化工作流设计。相比现有的 JavaScript 版本,本技能提供:
快速开始
1. 配置 API 密钥
提供 API 密钥的两种方式(按优先级排序):
1. **`.env` 文件**:在 `~/.openclaw/.env` 中添加(优先级最高)
```
BOCHA_API_KEY=sk-your-api-key
```
2. **环境变量**:设置 `BOCHA_API_KEY`
```bash
export BOCHA_API_KEY="sk-your-api-key"
```
> API 密钥获取:访问 https://open.bochaai.com → API KEY 管理
2. 基本搜索
# 使用位置参数
python3 scripts/search.py "沪电股份"
# 使用 --query 选项
python3 scripts/search.py --query "人工智能" --count 5
# 返回详细摘要
python3 scripts/search.py "DeepSeek" --summary
# 限制时间范围
python3 scripts/search.py "AI新闻" --freshness oneWeek --count 103. 输出格式
# 原始 JSON 格式(API 原始响应)
python3 scripts/search.py "阿里巴巴" --format raw
# Brave/Bing 兼容格式(默认,适合 AI 使用)
python3 scripts/search.py "阿里巴巴" --format brave
# Markdown 格式(人类可读)
python3 scripts/search.py "阿里巴巴" --format md高级用法
时间范围过滤
支持多种时间范围格式:
| 值 | 说明 | 示例 |
|----|------|------|
| `noLimit` | 不限时间(默认) | `--freshness noLimit` |
| `oneDay` | 一天内 | `--freshness oneDay` |
| `oneWeek` | 一周内 | `--freshness oneWeek` |
| `oneMonth` | 一个月内 | `--freshness oneMonth` |
| `oneYear` | 一年内 | `--freshness oneYear` |
| `YYYY-MM-DD` | 指定日期 | `--freshness 2025-04-06` |
| `YYYY-MM-DD..YYYY-MM-DD` | 日期范围 | `--freshness 2025-01-01..2025-04-06` |
示例:
# 搜索 2025 年 4 月的内容
python3 scripts/search.py "苹果发布会" --freshness 2025-04
# 搜索 2025 年第一季度内容
python3 scripts/search.py "财报" --freshness 2025-01-01..2025-03-31错误处理与重试
脚本内置错误处理和重试机制:
# 设置重试次数(默认 2 次)
python3 scripts/search.py "查询" --retries 3
# 设置超时时间(默认 30 秒)
python3 scripts/search.py "查询" --timeout 60自定义 API 端点
支持备用 API 端点:
# 使用备用端点
python3 scripts/search.py "查询" --endpoint "https://api.bocha.cn/v1/web-search"输出示例
Brave 兼容格式(默认)
{
"type": "search",
"query": "阿里巴巴",
"totalResults": 12345,
"resultCount": 10,
"results": [
{
"index": 1,
"title": "阿里巴巴发布2024年ESG报告",
"url": "https://www.alibabagroup.com/document...",
"description": "阿里巴巴集团发布《2024财年环境、社会和治理(ESG)报告》...",
"summary": "报告显示,阿里巴巴扎实推进减碳举措...",
"siteName": "阿里巴巴集团",
"publishedDate": "2024-07-22T00:00:00+08:00"
}
]
}Markdown 格式
## 搜索结果: 阿里巴巴
*找到约 12345 条结果*
1. **阿里巴巴发布2024年ESG报告**
*阿里巴巴集团*
[https://www.alibabagroup.com/document...](https://www.alibabagroup.com/document...)
阿里巴巴集团发布《2024财年环境、社会和治理(ESG)报告》...
*摘要*: 报告显示,阿里巴巴扎实推进减碳举措...
*发布时间*: 2024-07-22T00:00:00+08:00在 OpenClaw 中使用
直接调用脚本
# 从 OpenClaw workspace 根目录调用
python3 skills/bocha-search-python/scripts/search.py "查询"
# 使用绝对路径
python3 /root/.openclaw/workspace/skills/bocha-search-python/scripts/search.py "查询"集成到 Agent 工作流
在 Agent 的响应中调用搜索并处理结果:
# 示例:在 Python 代码中调用
import subprocess
import json
def bocha_search(query, count=5):
cmd = [
"python3",
"/root/.openclaw/workspace/skills/bocha-search-python/scripts/search.py",
"--query", query,
"--count", str(count),
"--format", "brave"
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
return json.loads(result.stdout)
else:
raise Exception(f"搜索失败: {result.stderr}")故障排除
常见错误
| 错误代码 | 原因 | 解决方案 |
|----------|------|----------|
| `INVALID_ARGUMENT` | 参数错误 | 检查查询关键词和参数格式 |
| `API_ERROR` | API 请求失败 | 检查 API 密钥和网络连接 |
| `UNKNOWN_ERROR` | 未知错误 | 查看详细错误信息并重试 |
调试模式
要查看更多调试信息,可以修改脚本或添加调试输出:
# 在 search.py 中添加调试
import logging
logging.basicConfig(level=logging.DEBUG)验证 API 密钥
# 简单测试 API 密钥是否有效
curl -X POST "https://api.bochaai.com/v1/web-search" \
-H "Authorization: Bearer YOUR-API-KEY" \
-H "Content-Type: application/json" \
-d '{"query": "test", "count": 1}'性能建议
1. **限制结果数量**:默认返回 10 条,根据需要调整
2. **使用适当的时间范围**:避免不必要的全文搜索
3. **缓存结果**:对重复查询考虑本地缓存
4. **批量处理**:避免频繁的单个请求
资源
scripts/search.py
主搜索脚本,包含所有核心功能。
版本历史
相关技能
---
**注意**:本技能需要有效的博查 API 密钥。使用前请确保已注册并获取密钥。
More tools from the same signal band
Order food/drinks (点餐) on an Android device paired as an OpenClaw node. Uses in-app menu and cart; add goods, view cart, submit order (demo, no real payment).
Sign plugins, rotate agent credentials without losing identity, and publicly attest to plugin behavior with verifiable claims and authenticated transfers.
The philosophical layer for AI agents. Maps behavior to Spinoza's 48 affects, calculates persistence scores, and generates geometric self-reports. Give your...