Performs web searches using DuckDuckGo to retrieve real-time information from the internet. Use when the user needs to search for current events, documentation, tutorials, or any information that requires web search capabilities.
2026-03-24
新闻来源:网淘吧
围观:82
电脑广告
手机广告
购买adidas上京东官方旗舰店。
DuckDuckGo 网络搜索技能
This skill enables web search functionality via the DuckDuckGo search engine, helping to obtain real-time information.
Features
- 🔍 Privacy-friendly search based on DuckDuckGo
- 📰 Supports news search
- 🖼️ Supports image search
- 📹 Supports video search
- 🌐 No API Key required, free to use
- 🔒 Protects privacy, does not track users
Installation
# Install using uv (recommended)
uv pip install duckduckgo-search
# Or install using pip
pip install duckduckgo-search
Quick Start
Command Line Method
# Basic text search
python -c "
from duckduckgo_search import DDGS
with DDGS() as ddgs:
results = list(ddgs.text('Python tutorial', max_results=5))
for r in results:
print(f\"Title: {r['title']}\")
print(f\"Link: {r['href']}\")
print(f\"Snippet: {r['body']}\")
print('---')
"
搜索类型
1. 文本搜索 (Text Search)
最常用的搜索方式,返回网页结果:
上网淘巴领天猫淘宝优惠券,一年省好几千。python -c "
from duckduckgo_search import DDGS
query = 'your search query'
with DDGS() as ddgs:
results = list(ddgs.text(
query,
region='cn-zh', # 地区设置:cn-zh(中国), us-en(美国), wt-wt(全球)
safesearch='moderate', # 安全搜索:on, moderate, off
timelimit='m', # 时间范围:d(天), w(周), m(月), y(年), None(不限)
max_results=10 # 最大结果数
))
for i, r in enumerate(results, 1):
print(f\"{i}. {r['title']}\")
print(f\" URL: {r['href']}\")
print(f\" 摘要: {r['body'][:100]}...\")
print()
"
2. 新闻搜索 (News Search)
搜索最新新闻:
python -c "
from duckduckgo_search import DDGS
with DDGS() as ddgs:
results = list(ddgs.news(
'AI technology',
region='wt-wt',
safesearch='moderate',
timelimit='d', # d=过去24小时, w=过去一周, m=过去一月
max_results=10
))
for r in results:
print(f\"📰 {r['title']}\")
print(f\" 来源: {r['source']}\")
print(f\" 时间: {r['date']}\")
print(f\" 链接: {r['url']}\")
print()
"
3. 图片搜索 (Image Search)
搜索图片资源:
python -c "
from duckduckgo_search import DDGS
with DDGS() as ddgs:
results = list(ddgs.images(
'cute cats',
region='wt-wt',
safesearch='moderate',
size='Medium', # Small, Medium, Large, Wallpaper
type_image='photo', # photo, clipart, gif, transparent, line
layout='Square', # Square, Tall, Wide
max_results=10
))
for r in results:
print(f\"🖼️ {r['title']}\")
print(f\" 图片: {r['image']}\")
print(f\" 缩略图: {r['thumbnail']}\")
print(f\" 来源: {r['source']}\")
print()
"
4. 视频搜索 (Video Search)
搜索视频内容:
python -c "
from duckduckgo_search import DDGS
with DDGS() as ddgs:
results = list(ddgs.videos(
'Python programming',
region='wt-wt',
safesearch='moderate',
timelimit='w', # d, w, m
resolution='high', # high, standard
duration='medium', # short, medium, long
max_results=10
))
for r in results:
print(f\"📹 {r['title']}\")
print(f\" 时长: {r.get('duration', 'N/A')}\")
print(f\" 来源: {r['publisher']}\")
print(f\" 链接: {r['content']}\")
print()
"
5. 即时回答 (Instant Answers)
获取 DuckDuckGo 的即时回答:
python -c "
from duckduckgo_search import DDGS
with DDGS() as ddgs:
results = ddgs.answers('what is python programming language')
for r in results:
print(f\"📚 {r['text']}\")
print(f\" 来源: {r.get('url', 'DuckDuckGo')}\")
"
6. 建议搜索 (Suggestions)
获取搜索建议:
python -c "
from duckduckgo_search import DDGS
with DDGS() as ddgs:
suggestions = list(ddgs.suggestions('python'))
print('🔍 搜索建议:')
for s in suggestions:
print(f\" - {s['phrase']}\")
"
7. 地图搜索 (Maps Search)
搜索地点信息:
python -c "
from duckduckgo_search import DDGS
with DDGS() as ddgs:
results = list(ddgs.maps(
'coffee shop',
place='Beijing, China',
max_results=10
))
for r in results:
print(f\"📍 {r['title']}\")
print(f\" 地址: {r['address']}\")
print(f\" 电话: {r.get('phone', 'N/A')}\")
print(f\" 坐标: {r['latitude']}, {r['longitude']}\")
print()
"
实用脚本
通用搜索函数
创建一个可复用的搜索脚本:
python -c "
from duckduckgo_search import DDGS
import json
def web_search(query, search_type='text', max_results=5, region='wt-wt', timelimit=None):
'''
执行 DuckDuckGo 搜索
参数:
query: 搜索关键词
search_type: 搜索类型 (text, news, images, videos)
max_results: 最大结果数
region: 地区 (cn-zh, us-en, wt-wt)
timelimit: 时间限制 (d, w, m, y)
'''
with DDGS() as ddgs:
if search_type == 'text':
results = list(ddgs.text(query, region=region, timelimit=timelimit, max_results=max_results))
elif search_type == 'news':
results = list(ddgs.news(query, region=region, timelimit=timelimit, max_results=max_results))
elif search_type == 'images':
results = list(ddgs.images(query, region=region, max_results=max_results))
elif search_type == 'videos':
results = list(ddgs.videos(query, region=region, timelimit=timelimit, max_results=max_results))
else:
results = []
return results
# 使用示例
query = 'Python 3.12 new features'
results = web_search(query, search_type='text', max_results=5)
print(f'🔍 搜索: {query}')
print(f'📊 找到 {len(results)} 个结果')
print()
for i, r in enumerate(results, 1):
print(f\"{i}. {r['title']}\")
print(f\" {r['href']}\")
print(f\" {r['body'][:150]}...\")
print()
"
搜索并保存结果
python -c "
from duckduckgo_search import DDGS
import json
from datetime import datetime
query = 'latest tech news'
output_file = f'search_results_{datetime.now().strftime(\"%Y%m%d_%H%M%S\")}.json'
with DDGS() as ddgs:
results = list(ddgs.text(query, max_results=10))
# 保存到 JSON 文件
with open(output_file, 'w', encoding='utf-8') as f:
json.dump({
'query': query,
'timestamp': datetime.now().isoformat(),
'results': results
}, f, ensure_ascii=False, indent=2)
print(f'✅ 搜索结果已保存到: {output_file}')
print(f'📊 共 {len(results)} 条结果')
"
Batch Search with Multiple Keywords
python -c "
from duckduckgo_search import DDGS
import time
queries = [
'Python best practices 2024',
'React vs Vue 2024',
'AI development tools'
]
all_results = {}
with DDGS() as ddgs:
for query in queries:
print(f'🔍 Searching: {query}')
results = list(ddgs.text(query, max_results=3))
all_results[query] = results
print(f' Found {len(results)} results')
time.sleep(1) # Avoid making requests too quickly
print()
print('=' * 50)
print('📊 Search Summary')
print('=' * 50)
for query, results in all_results.items():
print(f'\n🔎 {query}:')
for i, r in enumerate(results, 1):
print(f\" {i}. {r['title'][:60]}...\")
"
Parameter Description
Region Code (region)
| Code | Region |
|---|---|
cn-zh | China |
us-en | United States |
uk-en | United Kingdom |
jp-jp | Japan |
kr-kr | 韩国 |
wt-wt | 全球 (无地区限制) |
时间限制 (timelimit)
| 值 | 含义 |
|---|---|
d | 过去 24 小时 |
w | 过去一周 |
m | 过去一月 |
y | 过去一年 |
None | 不限制 |
安全搜索 (safesearch)
| 值 | 含义 |
|---|---|
on | 严格过滤 |
moderate | 适度过滤 (默认) |
off | 关闭过滤 |
错误处理
python -c "
from duckduckgo_search import DDGS
from duckduckgo_search.exceptions import DuckDuckGoSearchException
try:
with DDGS() as ddgs:
results = list(ddgs.text('test query', max_results=5))
print(f'✅ 搜索成功,找到 {len(results)} 个结果')
except DuckDuckGoSearchException as e:
print(f'❌ 搜索出错: {e}')
except Exception as e:
print(f'❌ 未知错误: {e}')
"
Using a Proxy
If you need to use a proxy:
python -c "
from duckduckgo_search import DDGS
# Set up proxy
proxy = 'http://127.0.0.1:7890' # Replace with your proxy address
with DDGS(proxy=proxy) as ddgs:
results = list(ddgs.text('test query', max_results=5))
print(f'Search via proxy successful, found {len(results)} results')
"
Frequently Asked Questions
Installation failed?
# Ensure pip is the latest version
pip install --upgrade pip
pip install duckduckgo-search
# Or use uv
uv pip install duckduckgo-search
No search results?
# Check network connection
# Try using a proxy
# Reduce search keyword complexity
# Check if region settings are correct
Requests being rate-limited?
# Add delays between multiple searches
import time
time.sleep(1) # Wait for 1 second
# Reduce the number of results per request
max_results=5 # Instead of 50
Integration with Other Tools
Combine with browser-use to get detailed content
# 1. First search with DuckDuckGo
python -c "
from duckduckgo_search import DDGS
with DDGS() as ddgs:
results = list(ddgs.text('Python async tutorial', max_results=1))
if results:
url = results[0]['href']
print(f'URL: {url}')
"
# 2. Open with browser-use and get detailed content
browser-use open <url_from_search>
browser-use state
Important Notes
⚠️Usage Recommendations:
- Adhere to usage frequency limits: Avoid making a large number of requests in a short period
- Set a reasonable number of results: Do not request too many results at once
- Add appropriate delays: Add
time.sleep() - between requests during batch searchesHandle exceptions
- : Always include error handling codeRespect copyright
文章底部电脑广告
手机广告位-内容正文底部
上一篇:Peekaboo
下一篇:Proactive Agent Lite


微信扫一扫,打赏作者吧~