介绍
# Web Search
## Overview
使用 DuckDuckGo 的 API 搜索网络,以查找网页、新闻文章、图片和视频中的信息。返回多种格式(文本、Markdown、JSON)的结果,并支持时间范围、地区和安全搜索的过滤选项。
## When to Use This Skill
当用户请求以下内容时使用此技能: - 搜索信息或资源的网络搜索 - 查找当前的或最新的在线信息 - 查找有关特定主题的新闻文章 - 根据描述或主题搜索图片 - 查找特定主题的视频 - 需要当前网络数据的研究 - 使用网络资源进行事实核查或验证 - 收集有关主题的 URL 和资源
## Prerequisites
安装所需的依赖项:
```bash pip install duckduckgo-search ```
该库提供了一个简单的 Python 接口来访问 DuckDuckGo 的搜索 API,无需 API 密钥或身份验证。
## Core Capabilities
### 1. Basic Web Search
搜索网页和信息:
```bash python scripts/search.py "<query>" ```
**示例:** ```bash python scripts/search.py "python asyncio tutorial" ```
以清晰的文本格式返回前 10 个网络结果,其中包含标题、URL 和描述。
### 2. Limiting Results
控制返回的结果数量:
```bash python scripts/search.py "<query>" --max-results <N> ```
**示例:** ```bash python scripts/search.py "machine learning frameworks" --max-results 20 ```
适用于: - 获取更全面的结果(增加限制) - 较少结果的快速查找(减少限制) - 平衡详细程度与处理时间
### 3. Time Range Filtering
按时间过滤结果:
```bash python scripts/search.py "<query>" --time-range <d|w|m|y> ```
**时间范围选项:** - `d` - 过去一天 - `w` - 过去一周 - `m` - 过去一个月 - `y` - 过去一年
**示例:** ```bash python scripts/search.py "artificial intelligence news" --time-range w ```
非常适合: - 查找最近的新闻或更新 - 过滤掉过时的内容 - 追踪近期的发展
### 4. News Search
专门搜索新闻文章:
```bash python scripts/search.py "<query>" --type news ```
**示例:** ```bash python scripts/search.py "climate change" --type news --time-range w --max-results 15 ```
新闻结果包括: - 文章标题 - 来源出版物 - 发布日期 - URL - 文章摘要/描述
### 5. Image Search
搜索图片:
```bash python scripts/search.py "<query>" --type images ```
**示例:** ```bash python scripts/search.py "sunset over mountains" --type images --max-results 20 ```
**图片过滤选项:**
尺寸过滤器: ```bash python scripts/search.py "landscape photos" --type images --image-size Large ``` 选项:`Small`、`Medium`、`Large`、`Wallpaper`
颜色过滤器: ```bash 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`
类型过滤器: ```bash python scripts/search.py "icons" --type images --image-type transparent ``` 选项:`photo`、`clipart`、`gif`、`transparent`、`line`
布局过滤器: ```bash python scripts/search.py "wallpapers" --type images --image-layout Wide ``` 选项:`Square`、`Tall`、`Wide`
图片结果包括: - 图片标题 - 图片 URL(指向图片的直接链接) - 缩略图 URL - 来源网站 - 尺寸(宽 x 高)
### 6. Video Search
搜索视频:
```bash python scripts/search.py "<query>" --type videos ```
**示例:** ```bash python scripts/search.py "python tutorial" --type videos --max-results 15 ```
**视频过滤选项:**
时长过滤器: ```bash python scripts/search.py "cooking recipes" --type videos --video-duration short ``` 选项:`short`、`medium`、`long`
分辨率过滤器: ```bash python scripts/search.py "documentary" --type videos --video-resolution high ``` 选项:`high`、`standard`
视频结果包括: - 视频标题 - 发布者/频道 - 时长 - 发布日期 - 视频 URL - 描述
### 7. Region-Specific Search
搜索特定地区的结果:
```bash python scripts/search.py "<query>" --region <region-code> ```
**常见地区代码:** - `us-en` - 美国(英语) - `uk-en` - 英国(英语) - `ca-en` - 加拿大(英语) - `au-en` - 澳大利亚(英语) - `de-de` - 德国(德语) - `fr-fr` - 法国(法语) - `wt-wt` - 全球(默认)
**示例:** ```bash python scripts/search.py "local news" --region us-en --type news ```
### 8. Safe Search Control
控制安全搜索过滤:
```bash python scripts/search.py "<query>" --safe-search <on|moderate|off> ```
**选项:** - `on` - 严格过滤 - `moderate` - 平衡过滤(默认) - `off` - 无过滤
**示例:** ```bash python scripts/search.py "medical information" --safe-search on ```
### 9. Output Formats
选择结果的格式:
**文本格式(默认):** ```bash python scripts/search.py "quantum computing" ```
清晰、可读的纯文本,带有编号的结果。
**Markdown 格式:** ```bash python scripts/search.py "quantum computing" --format markdown ```
带有标题、粗体文本和链接的格式化 Markdown。
**JSON 格式:** ```bash python scripts/search.py "quantum computing" --format json ```
用于程序化处理的结构化 JSON 数据。
### 10. Saving Results to File
将搜索结果保存到文件:
```bash python scripts/search.py "<query>" --output <file-path> ```
**示例:** ```bash 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` 标志决定,而不是文件扩展名。
## Output Format Examples
### Text 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 Format ```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 Format ```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..." } ] ```
## Common Usage Patterns
### Research on a Topic
收集有关主题的全面信息:
```bash # 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 ```
### Current Events Monitoring
跟踪有关特定主题的新闻:
```bash python scripts/search.py "climate summit" --type news --time-range d --format markdown --output daily_climate_news.md ```
### Finding Visual Resources
根据特定条件搜索图片:
```bash python scripts/search.py "data visualization examples" --type images --image-type photo --image-size Large --max-results 25 --output viz_images.txt ```
### Fact-Checking
使用最新来源核实信息:
```bash python scripts/search.py "specific claim to verify" --time-range w --max-results 20 ```
### Academic Research
查找有关学术主题的资源:
```bash python scripts/search.py "quantum entanglement research" --time-range y --max-results 30 --output quantum_research.txt ```
### Market Research
收集有关产品或公司的信息:
```bash 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 ```
## Implementation Approach
当用户请求网络搜索时:
1. **识别搜索意图**: - 什么类型的内容(网络、新闻、图片、视频)? - 结果应该多新? - 需要多少结果? - 有什么过滤要求吗?
2. **配置搜索参数**: - 选择合适的搜索类型(`--type`) - 如果时效性很重要,请设置时间范围(`--time-range`) - 调整结果数量(`--max-results`) - 应用过滤器(图片尺寸、视频时长等)
3. **选择输出格式**: - 用于快速阅读的文本 - 用于文档的 Markdown - 用于进一步处理的 JSON
4. **执行搜索**: - 运行搜索命令 - 如果需要保留结果,请保存到文件 - 打印到 stdout 以供立即查看
5. **处理结果**: - 如有必要,读取保存的文件 - 提取 URL 或特定信息 - 合并来自多次搜索的结果
## Quick Reference
**命令结构:** ```bash python scripts/search.py "<query>" [options] ```
**基本选项:** - `-t, --type` - 搜索类型(web、news、images、videos) - `-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) - `-o, --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)
**获取完整帮助:** ```bash python scripts/search.py --help ```
## Best Practices
1. **具体明确** - 使用清晰、具体的搜索查询以获得更好的结果 2. **使用时间过滤器** - 对当前信息应用 `--time-range` 3. **调整结果数量** - 从 10-20 个结果开始,根据需要增加 4. **保存重要的搜索** - 使用 `--output` 保留结果 5. **选择合适的类型** - 对时事使用新闻搜索,对一般信息使用网络搜索 6. **使用 JSON 进行自动化** - JSON 格式最容易通过编程方式解析 7. **合理使用** - 不要通过快速重复搜索来冲击 API
## Troubleshooting
**常见问题:**
- **“Missing required dependency”**:运行 `pip install duckduckgo-search` - **未找到结果**:尝试使用更宽泛的搜索词或删除时间过滤器 - **超时错误**:搜索服务可能暂时不可用;请稍后重试 - **速率限制**:如果发出许多请求,请错开搜索时间 - **意外结果**:DuckDuckGo 的结果可能与 Google 不同;尝试优化查询
**局限性:**
- 结果质量取决于 DuckDuckGo 的索引和算法 - 没有高级搜索运算符(不像 Google 的 site:、filetype: 等) - 图片和视频搜索的结果可能少于网络搜索 - 无法控制结果排名或相关性评分 - 某些专门的搜索可能在专用搜索引擎上效果更好
## Advanced Use Cases
### Combining Multiple Searches
通过组合搜索类型收集全面信息:
```bash # 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 ```
### Programmatic Processing
使用 JSON 输出进行自动处理:
```bash python scripts/search.py "research topic" --format json --output results.json # Then process with another script python analyze_results.py results.json ```
### Building a Knowledge Base
根据网络结果创建可搜索的文档:
```bash # 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 ```
## Resources
### scripts/search.py
实现 DuckDuckGo 搜索功能的主要搜索工具。主要功能:
- **多种搜索类型** - 网络、新闻、图片和视频 - **灵活的过滤** - 时间范围、地区、安全搜索和特定类型的过滤器 - **多种输出格式** - 文本、Markdown 和 JSON - **文件输出** - 保存结果以供稍后处理 - **清晰的格式** - 包含所有基本信息的可读输出 - **错误处理** - 优雅地处理网络错误和空结果
该脚本可以直接执行,并通过 `--help` 包含全面的命令行帮助。