|
| 1 | +# DuckDuckGo 搜索工具 |
| 2 | + |
| 3 | +基于 `duckduckgo_search` 库实现的搜索工具,支持文字搜索和图片搜索。无需API密钥,开箱即用,可集成到MaxKB知识库系统中。 |
| 4 | + |
| 5 | +## 一、功能说明 |
| 6 | + |
| 7 | +- **文字搜索**:获取最新的网络信息和数据 |
| 8 | +- **图片搜索**:搜索并获取图片资源 |
| 9 | +- **隐私保护**:DuckDuckGo不跟踪用户搜索记录 |
| 10 | +- **免费使用**:无需API密钥,完全免费 |
| 11 | + |
| 12 | +## 二、参数说明 |
| 13 | + |
| 14 | +### 2.1 输入参数 |
| 15 | + |
| 16 | +| 参数名 | 数据类型 | 必填 | 默认值 | 说明 | |
| 17 | +| :--- | :--- | :--- | :--- | :--- | |
| 18 | +| `query` | string | 是 | - | 搜索关键词 | |
| 19 | +| `max_results` | int | 否 | `10` | 返回的最大结果数量(1-20) | |
| 20 | +| `search_type` | string | 否 | `text` | 搜索类型:`text`(文字搜索) 或 `image`(图片搜索) | |
| 21 | +| `region` | string | 否 | `cn-zh` | 搜索区域,如 `cn-zh`(中国)、`us-en`(美国)、`uk-en`(英国) 等 | |
| 22 | +| `timeout` | int | 否 | `15` | 请求超时时间(秒) | |
| 23 | +| `proxy` | string | 否 | `None` | 代理服务器地址,格式如 `http://proxy.example.com:8080` | |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## 三、工具内容(Python) |
| 28 | + |
| 29 | +```python |
| 30 | +from ddgs import DDGS |
| 31 | +import json |
| 32 | + |
| 33 | + |
| 34 | +def duckduckgo_search( |
| 35 | + query, max_results=10, search_type="text", region="cn-zh", timeout=15, proxy=None |
| 36 | +): |
| 37 | + """ |
| 38 | + 使用 DuckDuckGo 进行搜索(统一入口函数) |
| 39 | +
|
| 40 | + 参数: |
| 41 | + query: 搜索关键词 |
| 42 | + max_results: 返回的最大结果数量,默认10条 |
| 43 | + search_type: 搜索类型,'text'(文字搜索) 或 'image'(图片搜索),默认'text' |
| 44 | +
|
| 45 | + 返回: |
| 46 | + JSON格式的搜索结果列表 |
| 47 | + """ |
| 48 | + try: |
| 49 | + # 确保 max_results 在合理范围内 |
| 50 | + max_results = int(max_results) |
| 51 | + if max_results < 1: |
| 52 | + max_results = 1 |
| 53 | + if max_results > 20: |
| 54 | + max_results = 20 |
| 55 | + # 创建 DDGS 实例并执行搜索 |
| 56 | + ddgs = DDGS(timeout=timeout, proxy=proxy) |
| 57 | + if search_type == "image": |
| 58 | + results = list( |
| 59 | + ddgs.images( |
| 60 | + query=query, |
| 61 | + region=region, |
| 62 | + max_results=max_results, |
| 63 | + ) |
| 64 | + ) |
| 65 | + else: |
| 66 | + results = list( |
| 67 | + ddgs.text(query=query, region=region, max_results=max_results) |
| 68 | + ) |
| 69 | + |
| 70 | + # 返回 JSON 格式的结果 |
| 71 | + return json.dumps(results, ensure_ascii=False, indent=2) |
| 72 | + |
| 73 | + except Exception as e: |
| 74 | + error_result = { |
| 75 | + "error": str(e), |
| 76 | + "message": f"{search_type}搜索失败,请检查网络连接或稍后重试", |
| 77 | + } |
| 78 | + return json.dumps(error_result, ensure_ascii=False, indent=2) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + query = "猫" |
| 83 | + max_results = 5 |
| 84 | + search_type = "image" |
| 85 | + region = "cn-zh" |
| 86 | + timeout = 15 |
| 87 | + result = duckduckgo_search(query, max_results, search_type, region, timeout) |
| 88 | + print(result) |
| 89 | + |
| 90 | +``` |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +## 四、使用示例 |
| 95 | + |
| 96 | +### 文字搜索 |
| 97 | + |
| 98 | +```python |
| 99 | +# 基本搜索 |
| 100 | +result = duckduckgo_search(query='Python编程') |
| 101 | + |
| 102 | +# 指定返回结果数量 |
| 103 | +result = duckduckgo_search(query='AI新闻', max_results=5) |
| 104 | + |
| 105 | +# 明确指定文字搜索 |
| 106 | +result = duckduckgo_search(query='Python编程', max_results=10, search_type='text') |
| 107 | +``` |
| 108 | + |
| 109 | +### 图片搜索 |
| 110 | + |
| 111 | +```python |
| 112 | +# 基本图片搜索 |
| 113 | +images = duckduckgo_search(query='风景', search_type='image') |
| 114 | + |
| 115 | +# 指定返回结果数量 |
| 116 | +images = duckduckgo_search(query='猫', max_results=20, search_type='image') |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## 五、依赖库 |
| 122 | + |
| 123 | +| 依赖库 | 版本要求 | 用途说明 | |
| 124 | +| :--- | :--- | :--- | |
| 125 | +| `ddgs` | ≥ 9.10.0 | DuckDuckGo搜索核心库 | |
| 126 | + |
| 127 | +安装命令: |
| 128 | + |
| 129 | +```bash |
| 130 | +pip install ddgs |
| 131 | +``` |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## 六、注意事项 |
| 136 | + |
| 137 | +- 确保网络连接正常 |
| 138 | +- 遵守DuckDuckGo使用条款 |
| 139 | +- 建议合理控制搜索频率,避免被限流 |
| 140 | +- 搜索结果的准确性需要人工验证 |
0 commit comments