name: web-search description: 此技能适用于用户需要在网上搜索信息、查找最新内容、查阅新闻文章、搜索图片或视频的情况。它通过使用 DuckDuckGo 的搜索 API 来返回格式化良好的搜索结果(文本、Markdown 或 JSON 格式)。适用于研究、事实核查、查找最新信息或收集网络资源等场景。
网页搜索
概述
使用 DuckDuckGo 的 API 在网页、新闻文章、图片和视频中搜索信息。返回的结果支持多种格式(文本、Markdown、JSON),并提供时间范围、地区和安全搜索等过滤选项。
适用场景
当用户需要执行以下操作时,可以使用此功能:
- 在网络上搜索信息或资源
- 查找当前或最近的新闻文章
- 根据描述或主题搜索图片
- 搜索特定主题的视频
- 进行需要最新网络数据的研究
- 通过网络来源进行事实核查或验证
- 收集关于某个主题的 URL 和资源
先决条件
安装所需的依赖项:
pip install duckduckgo-search
该库提供了一个简单的 Python 接口,用于调用 DuckDuckGo 的搜索 API,无需 API 密钥或身份验证。
核心功能
1. 基本网页搜索
搜索网页和信息:
python scripts/search.py "<query>"
示例:
python scripts/search.py "python asyncio tutorial"
以清晰的文本格式返回前 10 个网页结果,包括标题、URL 和描述。
2. 限制结果数量
控制返回的结果数量:
python scripts/search.py "<query>" --max-results <N>
示例:
python scripts/search.py "machine learning frameworks" --max-results 20
用途:
- 获取更全面的结果(增加限制数量)
- 快速查找(减少结果数量)
- 在详细程度和处理时间之间取得平衡
3. 时间范围过滤
按时间顺序过滤结果:
python scripts/search.py "<query>" --time-range <d|w|m|y>
时间范围选项:
d- 过去一天w- 过去一周m- 过去一个月y- 过去一年
示例:
python scripts/search.py "artificial intelligence news" --time-range w
适用场景:
- 查找最新的新闻或更新
- 过滤过时的内容
- 跟踪最新发展
4. 新闻搜索
专门搜索新闻文章:
python scripts/search.py "<query>" --type news
示例:
python scripts/search.py "climate change" --type news --time-range w --max-results 15
新闻结果包括:
- 文章标题
- 来源出版物
- 发布日期
- URL
- 文章摘要/描述
5. 图片搜索
搜索图片:
python scripts/search.py "<query>" --type images
示例:
python scripts/search.py "sunset over mountains" --type images --max-results 20
图片过滤选项:
尺寸过滤:
python scripts/search.py "landscape photos" --type images --image-size Large
选项:Small(小)、Medium(中)、Large(大)、Wallpaper(壁纸)
颜色过滤:
python scripts/search.py "abstract art" --type images --image-color Blue
选项:color(彩色)、Monochrome(黑白)、Red(红色)、Orange(橙色)、Yellow(黄色)、Green(绿色)、Blue(蓝色)、Purple(紫色)、Pink(粉色)、Brown(棕色)、Black(黑色)、Gray(灰色)、Teal(青色)、White(白色)
类型过滤:
python scripts/search.py "icons" --type images --image-type transparent
选项:photo(照片)、clipart(剪贴画)、gif(动图)、transparent(透明图)、line(线条图)
布局过滤:
python scripts/search.py "wallpapers" --type images --image-layout Wide
选项:Square(正方形)、Tall(高分辨率)、Wide(宽屏)
图片结果包括:
- 图片标题
- 图片 URL(直接链接)
- 缩略图 URL
- 来源网站
- 尺寸(宽度 x 高度)
6. 视频搜索
搜索视频:
python scripts/search.py "<query>" --type videos
示例:
python scripts/search.py "python tutorial" --type videos --max-results 15
视频过滤选项:
时长过滤:
python scripts/search.py "cooking recipes" --type videos --video-duration short
选项:short(短片)、medium(中等时长)、long(长片)
分辨率过滤:
python scripts/search.py "documentary" --type videos --video-resolution high
选项:high(高清)、standard(标准分辨率)
视频结果包括:
- 视频标题
- 出版商/频道
- 时长
- 发布日期
- 视频 URL
- 描述
7. 地区特定搜索
根据地区筛选结果:
python scripts/search.py "<query>" --region <region-code>
常见地区代码:
us-en- 美国(英语)uk-en- 英国(英语)ca-en- 加拿大(英语)au-en- 澳大利亚(英语)de-de- 德国(德语)fr-fr- 法国(法语)wt-wt- 全球(默认)
示例:
python scripts/search.py "local news" --region us-en --type news
8. 安全搜索控制
控制安全搜索的过滤级别:
python scripts/search.py "<query>" --safe-search <on|moderate|off>
选项:
on(严格过滤)moderate(中等过滤,默认值)off(不进行过滤)
示例:
python scripts/search.py "medical information" --safe-search on
9. 输出格式
选择结果的显示格式:
文本格式(默认):
python scripts/search.py "quantum computing"
简洁易读的纯文本格式,结果按顺序编号。
Markdown 格式:
python scripts/search.py "quantum computing" --format markdown
带有标题、加粗文本和链接的格式化 Markdown。
JSON 格式:
python scripts/search.py "quantum computing" --format json
结构化的 JSON 数据,适用于程序化处理。
10. 将结果保存到文件
将搜索结果保存到文件:
python scripts/search.py "<query>" --output <file-path>
示例:
python scripts/search.py "artificial intelligence" --output ai_results.txt
python scripts/search.py "AI news" --type news --format markdown --output ai_news.md
python scripts/search.py "AI research" --format json --output ai_data.json
文件格式由 --format 参数决定,与文件扩展名无关。
输出格式示例
文本格式
1. Page Title Here
URL: https://example.com/page
Brief description of the page content...
2. Another Result
URL: https://example.com/another
Another description...
Markdown 格式
## 1. Page Title Here
**URL:** https://example.com/page
Brief description of the page content...
## 2. Another Result
**URL:** https://example.com/another
Another description...
JSON 格式
[
{
"title": "Page Title Here",
"href": "https://example.com/page",
"body": "Brief description of the page content..."
},
{
"title": "Another Result",
"href": "https://example.com/another",
"body": "Another description..."
}
]
常见使用场景
主题研究
收集关于某个主题的全面信息:
# Get overview from web
python scripts/search.py "machine learning basics" --max-results 15 --output ml_web.txt
# Get recent news
python scripts/search.py "machine learning" --type news --time-range m --output ml_news.txt
# Find tutorial videos
python scripts/search.py "machine learning tutorial" --type videos --max-results 10 --output ml_videos.txt
监控时事
跟踪特定主题的新闻:
python scripts/search.py "climate summit" --type news --time-range d --format markdown --output daily_climate_news.md
查找视觉资源
根据特定条件搜索图片:
python scripts/search.py "data visualization examples" --type images --image-type photo --image-size Large --max-results 25 --output viz_images.txt
事实核查
使用最新来源验证信息:
python scripts/search.py "specific claim to verify" --time-range w --max-results 20
学术研究
查找学术主题的相关资源:
python scripts/search.py "quantum entanglement research" --time-range y --max-results 30 --output quantum_research.txt
市场研究
收集关于产品或公司的信息:
python scripts/search.py "electric vehicle market 2025" --max-results 20 --format markdown --output ev_market.md
python scripts/search.py "EV news" --type news --time-range m --output ev_news.txt
实现方法
当用户请求网页搜索时:
- 确定搜索意图:
- 需要哪种类型的内容(网页、新闻、图片、视频)?
- 结果需要多新? 有任何过滤要求吗?
- 配置搜索参数:
- 选择合适的搜索类型(
--type) - 如果需要按时间筛选,请设置时间范围(
--time-range) - 调整结果数量(
--max-results) - 应用过滤条件(图片尺寸、视频时长等)
- 选择合适的搜索类型(
- 选择输出格式:
- 选择文本格式以便快速阅读
- 选择 Markdown 格式用于文档编写
- 选择 JSON 格式以便进一步处理
- 执行搜索:
- 运行搜索命令
- 如果需要保存结果,请将其保存到文件
- 将结果输出到标准输出(stdout)以便立即查看
- 处理结果:
- 如有需要,读取保存的文件
- 提取 URL 或特定信息
- 合并多次搜索的结果
快速参考
命令结构:
python scripts/search.py "<query>" [options]
必备选项:
-t, --type- 搜索类型(网页、新闻、图片、视频)-n, --max-results- 最大结果数量(默认:10)--time-range- 时间范围(d、w、m、y)--r, --region- 地区代码(例如:us-en、uk-en)--safe-search- 安全搜索级别(on、moderate、off)--f, --format- 输出格式(text、markdown、json)--output- 保存到文件
图片相关选项:
--image-size- 图片尺寸(Small、Medium、Large、Wallpaper)--image-color- 图片颜色--image-type- 图片类型(photo、clipart、gif、transparent、line)--image-layout- 图片布局(Square、Tall、Wide)
视频相关选项:
--video-duration- 视频时长(short、medium、long)--video-resolution- 视频分辨率(high、standard)
获取完整帮助:
python scripts/search.py --help
最佳实践
- 明确搜索需求 - 使用具体、清晰的查询语句以获得更好的结果
- 使用时间过滤 - 通过
--time-range获取最新信息 - 调整结果数量 - 初始设置 10-20 个结果,根据需要增加
- 保存重要搜索结果 - 使用
--output保存结果 - 选择合适的格式 - 使用新闻搜索获取最新信息,使用网页搜索获取一般信息
- 选择 JSON 格式 - JSON 格式最便于程序化处理
- 合理使用 API - 避免频繁重复请求导致 API 压力过大
故障排除
常见问题:
- “缺少所需依赖项”:运行
pip install duckduckgo-search - 未找到结果:尝试使用更宽泛的搜索词或移除时间过滤条件
- 超时错误:搜索服务可能暂时不可用,请稍后重试
- 请求限制:如果频繁请求,请间隔一段时间
- 结果异常:DuckDuckGo 的结果可能与 Google 不同,请尝试细化查询
限制因素:
- 结果质量取决于 DuckDuckGo 的索引和算法
- 不提供高级搜索操作符(如 Google 的
site:、filetype:等) - 图片和视频搜索的结果可能少于网页搜索
- 无法控制结果排名或相关性评分
- 某些特定搜索可能在专用搜索引擎上效果更好
高级用法
结合多种搜索方式
通过结合多种搜索类型收集全面信息:
# Web overview
python scripts/search.py "topic" --max-results 15 --output topic_web.txt
# Recent news
python scripts/search.py "topic" --type news --time-range w --output topic_news.txt
# Images
python scripts/search.py "topic" --type images --max-results 20 --output topic_images.txt
程序化处理
使用 JSON 格式进行自动化处理:
python scripts/search.py "research topic" --format json --output results.json
# Then process with another script
python analyze_results.py results.json
构建知识库
从网页结果中创建可搜索的文档:
# Search multiple related topics
python scripts/search.py "topic1" --format markdown --output kb/topic1.md
python scripts/search.py "topic2" --format markdown --output kb/topic2.md
python scripts/search.py "topic3" --format markdown --output kb/topic3.md
资源
scripts/search.py
这个主要的搜索工具实现了 DuckDuckGo 的搜索功能。主要特点包括:
- 多种搜索类型:网页、新闻、图片和视频
- 灵活的过滤:时间范围、地区、安全搜索和特定类型过滤
- 多种输出格式:文本、Markdown 和 JSON
- 文件输出:保存结果以供后续处理
- 清晰的格式:易于阅读的格式,包含所有必要信息
- 错误处理:优雅地处理网络错误和空结果
该脚本可以直接执行,并通过 --help 提供详细的命令行帮助。