Skip to content

Commit 7a2f9cf

Browse files
author
halo
committed
feat: 支持 DuckDuckGo 搜索工具
1 parent 910c9b8 commit 7a2f9cf

5 files changed

Lines changed: 180 additions & 0 deletions

File tree

2.7 KB
Binary file not shown.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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` | 返回的最大结果数量 |
20+
| `search_type` | string || `text` | 搜索类型:`text`(文字搜索) 或 `image`(图片搜索) |
21+
22+
---
23+
24+
## 三、工具内容(Python)
25+
26+
```python
27+
from ddgs import DDGS
28+
import json
29+
30+
31+
def duckduckgo_search(query, max_results=10, search_type='text'):
32+
"""
33+
使用 DuckDuckGo 进行搜索(统一入口函数)
34+
35+
参数:
36+
query: 搜索关键词
37+
max_results: 返回的最大结果数量,默认10条
38+
search_type: 搜索类型,'text'(文字搜索) 或 'image'(图片搜索),默认'text'
39+
40+
返回:
41+
JSON格式的搜索结果列表
42+
"""
43+
try:
44+
# 确保 max_results 在合理范围内
45+
max_results = int(max_results)
46+
if max_results < 1:
47+
max_results = 1
48+
if max_results > 20:
49+
max_results = 20
50+
# 创建 DDGS 实例并执行搜索
51+
with DDGS() as ddgs:
52+
if search_type == 'image':
53+
results = list(ddgs.images(
54+
keywords=query,
55+
max_results=max_results
56+
))
57+
else:
58+
results = list(ddgs.text(
59+
keywords=query,
60+
max_results=max_results
61+
))
62+
63+
# 返回 JSON 格式的结果
64+
return json.dumps(results, ensure_ascii=False, indent=2)
65+
66+
except Exception as e:
67+
error_result = {
68+
"error": str(e),
69+
"message": f"{search_type}搜索失败,请检查网络连接或稍后重试"
70+
}
71+
return json.dumps(error_result, ensure_ascii=False, indent=2)
72+
```
73+
74+
---
75+
76+
## 四、使用示例
77+
78+
### 文字搜索
79+
80+
```python
81+
# 基本搜索
82+
result = duckduckgo_search(query='Python编程')
83+
84+
# 指定返回结果数量
85+
result = duckduckgo_search(query='AI新闻', max_results=5)
86+
87+
# 明确指定文字搜索
88+
result = duckduckgo_search(query='Python编程', max_results=10, search_type='text')
89+
```
90+
91+
### 图片搜索
92+
93+
```python
94+
# 基本图片搜索
95+
images = duckduckgo_search(query='风景', search_type='image')
96+
97+
# 指定返回结果数量
98+
images = duckduckgo_search(query='', max_results=20, search_type='image')
99+
```
100+
101+
---
102+
103+
## 五、依赖库
104+
105+
| 依赖库 | 版本要求 | 用途说明 |
106+
| :--- | :--- | :--- |
107+
| `ddgs` | ≥ 9.10.0 | DuckDuckGo搜索核心库 |
108+
109+
安装命令:
110+
111+
```bash
112+
pip install ddgs
113+
```
114+
115+
---
116+
117+
## 六、注意事项
118+
119+
- 确保网络连接正常
120+
- 遵守DuckDuckGo使用条款
121+
- 建议合理控制搜索频率,避免被限流
122+
- 搜索结果的准确性需要人工验证
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: DuckDuckGo搜索
2+
tags:
3+
- 联网搜索
4+
title: 基于DuckDuckGo的免费联网搜索工具,无需API密钥即可使用
5+
description: 基于DuckDuckGo的免费联网搜索工具,无需API密钥即可使用
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from ddgs import DDGS
2+
import json
3+
4+
5+
def duckduckgo_search(query, max_results=10, search_type="text", region="cn-zh"):
6+
"""
7+
使用 DuckDuckGo 进行搜索(统一入口函数)
8+
9+
参数:
10+
query: 搜索关键词
11+
max_results: 返回的最大结果数量,默认10条
12+
search_type: 搜索类型,'text'(文字搜索) 或 'image'(图片搜索),默认'text'
13+
14+
返回:
15+
JSON格式的搜索结果列表
16+
"""
17+
try:
18+
# 确保 max_results 在合理范围内
19+
max_results = int(max_results)
20+
if max_results < 1:
21+
max_results = 1
22+
if max_results > 20:
23+
max_results = 20
24+
# 创建 DDGS 实例并执行搜索
25+
ddgs = DDGS()
26+
if search_type == "image":
27+
results = list(
28+
ddgs.images(
29+
query=query,
30+
max_results=max_results,
31+
region=region,
32+
safesearch="off",
33+
)
34+
)
35+
else:
36+
results = list(
37+
ddgs.text(
38+
query=query,
39+
max_results=max_results,
40+
region=region,
41+
safesearch="off",
42+
)
43+
)
44+
45+
# 返回 JSON 格式的结果
46+
return json.dumps(results, ensure_ascii=False, indent=2)
47+
48+
except Exception as e:
49+
error_result = {
50+
"error": str(e),
51+
"message": f"{search_type}搜索失败,请检查网络连接或稍后重试",
52+
}
53+
return json.dumps(error_result, ensure_ascii=False, indent=2)
22.4 KB
Loading

0 commit comments

Comments
 (0)