Skip to content

Commit 7c7b2ec

Browse files
authored
Merge pull request #53 from Halo1236/ddgs
feat: 支持 DuckDuckGo 搜索工具
2 parents 506996d + a67ec81 commit 7c7b2ec

5 files changed

Lines changed: 204 additions & 0 deletions

File tree

3.08 KB
Binary file not shown.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
- 搜索结果的准确性需要人工验证
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from ddgs import DDGS
2+
import json
3+
4+
5+
def duckduckgo_search(
6+
query, max_results=10, search_type="text", region="cn-zh", timeout=15, proxy=None
7+
):
8+
"""
9+
使用 DuckDuckGo 进行搜索(统一入口函数)
10+
11+
参数:
12+
query: 搜索关键词
13+
max_results: 返回的最大结果数量,默认10条
14+
search_type: 搜索类型,'text'(文字搜索) 或 'image'(图片搜索),默认'text'
15+
16+
返回:
17+
JSON格式的搜索结果列表
18+
"""
19+
try:
20+
# 确保 max_results 在合理范围内
21+
max_results = int(max_results)
22+
if max_results < 1:
23+
max_results = 1
24+
if max_results > 20:
25+
max_results = 20
26+
# 创建 DDGS 实例并执行搜索
27+
ddgs = DDGS(timeout=timeout, proxy=proxy)
28+
if search_type == "image":
29+
results = list(
30+
ddgs.images(
31+
query=query,
32+
region=region,
33+
max_results=max_results,
34+
)
35+
)
36+
else:
37+
results = list(
38+
ddgs.text(query=query, region=region, max_results=max_results)
39+
)
40+
41+
# 返回 JSON 格式的结果
42+
return json.dumps(results, ensure_ascii=False, indent=2)
43+
44+
except Exception as e:
45+
error_result = {
46+
"error": str(e),
47+
"message": f"{search_type}搜索失败,请检查网络连接或稍后重试",
48+
}
49+
return json.dumps(error_result, ensure_ascii=False, indent=2)
50+
51+
52+
if __name__ == "__main__":
53+
query = "猫"
54+
max_results = 5
55+
search_type = "image"
56+
region = "cn-zh"
57+
timeout = 15
58+
result = duckduckgo_search(query, max_results, search_type, region, timeout)
59+
print(result)
22.4 KB
Loading

0 commit comments

Comments
 (0)