Web Search
Overview
Search the web using DuckDuckGo's API to find information across web pages, news articles, images, and videos. Returns results in multiple formats (text, markdown, JSON) with filtering options for time range, region, and safe search.
When to Use This Skill
Use this skill when users request:
- - Web searches for information or resources
- Finding current or recent information online
- Looking up news articles about specific topics
- Searching for images by description or topic
- Finding videos on specific subjects
- Research requiring current web data
- Fact-checking or verification using web sources
- Gathering URLs and resources on a topic
Prerequisites
Install the required dependency:
CODEBLOCK0
This library provides a simple Python interface to DuckDuckGo's search API without requiring API keys or authentication.
Core Capabilities
1. Basic Web Search
Search for web pages and information:
CODEBLOCK1
Example:
CODEBLOCK2
Returns the top 10 web results with titles, URLs, and descriptions in a clean text format.
2. Limiting Results
Control the number of results returned:
CODEBLOCK3
Example:
CODEBLOCK4
Useful for:
- - Getting more comprehensive results (increase limit)
- Quick lookups with fewer results (decrease limit)
- Balancing detail vs. processing time
3. Time Range Filtering
Filter results by recency:
CODEBLOCK5
Time range options:
- -
d - Past day - INLINECODE1 - Past week
- INLINECODE2 - Past month
- INLINECODE3 - Past year
Example:
CODEBLOCK6
Great for:
- - Finding recent news or updates
- Filtering out outdated content
- Tracking recent developments
4. News Search
Search specifically for news articles:
CODEBLOCK7
Example:
CODEBLOCK8
News results include:
- - Article title
- Source publication
- Publication date
- URL
- Article summary/description
5. Image Search
Search for images:
CODEBLOCK9
Example:
CODEBLOCK10
Image filtering options:
Size filters:
python scripts/search.py "landscape photos" --type images --image-size Large
Options:
Small,
Medium,
Large, INLINECODE7
Color filters:
python scripts/search.py "abstract art" --type images --image-color Blue
Options:
color,
Monochrome,
Red,
Orange,
Yellow,
Green,
Blue,
Purple,
Pink,
Brown,
Black,
Gray,
Teal, INLINECODE21
Type filters:
python scripts/search.py "icons" --type images --image-type transparent
Options:
photo,
clipart,
gif,
transparent, INLINECODE26
Layout filters:
python scripts/search.py "wallpapers" --type images --image-layout Wide
Options:
Square,
Tall, INLINECODE29
Image results include:
- - Image title
- Image URL (direct link to image)
- Thumbnail URL
- Source website
- Dimensions (width x height)
6. Video Search
Search for videos:
CODEBLOCK15
Example:
CODEBLOCK16
Video filtering options:
Duration filters:
python scripts/search.py "cooking recipes" --type videos --video-duration short
Options:
short,
medium, INLINECODE32
Resolution filters:
python scripts/search.py "documentary" --type videos --video-resolution high
Options:
high, INLINECODE34
Video results include:
- - Video title
- Publisher/channel
- Duration
- Publication date
- Video URL
- Description
7. Region-Specific Search
Search with region-specific results:
CODEBLOCK19
Common region codes:
- -
us-en - United States (English) - INLINECODE36 - United Kingdom (English)
- INLINECODE37 - Canada (English)
- INLINECODE38 - Australia (English)
- INLINECODE39 - Germany (German)
- INLINECODE40 - France (French)
- INLINECODE41 - Worldwide (default)
Example:
CODEBLOCK20
8. Safe Search Control
Control safe search filtering:
CODEBLOCK21
Options:
- -
on - Strict filtering - INLINECODE43 - Balanced filtering (default)
- INLINECODE44 - No filtering
Example:
CODEBLOCK22
9. Output Formats
Choose how results are formatted:
Text format (default):
CODEBLOCK23
Clean, readable plain text with numbered results.
Markdown format:
CODEBLOCK24
Formatted markdown with headers, bold text, and links.
JSON format:
CODEBLOCK25
Structured JSON data for programmatic processing.
10. Saving Results to File
Save search results to a file:
CODEBLOCK26
Example:
CODEBLOCK27
The file format is determined by the --format flag, not the file extension.
Output Format Examples
Text Format
CODEBLOCK28
Markdown Format
CODEBLOCK29
JSON Format
CODEBLOCK30
Common Usage Patterns
Research on a Topic
Gather comprehensive information about a subject:
CODEBLOCK31
Current Events Monitoring
Track news on specific topics:
CODEBLOCK32
Finding Visual Resources
Search for images with specific criteria:
CODEBLOCK33
Fact-Checking
Verify information with recent sources:
CODEBLOCK34
Academic Research
Find resources on scholarly topics:
CODEBLOCK35
Market Research
Gather information about products or companies:
CODEBLOCK36
Implementation Approach
When users request web searches:
- 1. Identify search intent:
- What type of content (web, news, images, videos)?
- How recent should results be?
- How many results are needed?
- Any filtering requirements?
- 2. Configure search parameters:
- Choose appropriate search type (
--type)
- Set time range if currency matters (
--time-range)
- Adjust result count (
--max-results)
- Apply filters (image size, video duration, etc.)
- 3. Select output format:
- Text for quick reading
- Markdown for documentation
- JSON for further processing
- 4. Execute search:
- Run the search command
- Save to file if results need to be preserved
- Print to stdout for immediate review
- 5. Process results:
- Read saved files if needed
- Extract URLs or specific information
- Combine results from multiple searches
Quick Reference
Command structure:
CODEBLOCK37
Essential options:
- -
-t, --type - Search type (web, news, images, videos) - INLINECODE50 - Maximum results (default: 10)
- INLINECODE51 - Time filter (d, w, m, y)
- INLINECODE52 - Region code (e.g., us-en, uk-en)
- INLINECODE53 - Safe search level (on, moderate, off)
- INLINECODE54 - Output format (text, markdown, json)
- INLINECODE55 - Save to file
Image-specific options:
- -
--image-size - Size filter (Small, Medium, Large, Wallpaper) - INLINECODE57 - Color filter
- INLINECODE58 - Type filter (photo, clipart, gif, transparent, line)
- INLINECODE59 - Layout filter (Square, Tall, Wide)
Video-specific options:
- -
--video-duration - Duration filter (short, medium, long) - INLINECODE61 - Resolution filter (high, standard)
Get full help:
CODEBLOCK38
Best Practices
- 1. Be specific - Use clear, specific search queries for better results
- Use time filters - Apply
--time-range for current information - Adjust result count - Start with 10-20 results, increase if needed
- Save important searches - Use
--output to preserve results - Choose appropriate type - Use news search for current events, web for general info
- Use JSON for automation - JSON format is easiest to parse programmatically
- Respect usage - Don't hammer the API with rapid repeated searches
Troubleshooting
Common issues:
- - "Missing required dependency": Run INLINECODE64
- No results found: Try broader search terms or remove time filters
- Timeout errors: The search service may be temporarily unavailable; retry after a moment
- Rate limiting: Space out searches if making many requests
- Unexpected results: DuckDuckGo's results may differ from Google; try refining the query
Limitations:
- - Results quality depends on DuckDuckGo's index and algorithms
- No advanced search operators (unlike Google's site:, filetype:, etc.)
- Image and video searches may have fewer results than web search
- No control over result ranking or relevance scoring
- Some specialized searches may work better on dedicated search engines
Advanced Use Cases
Combining Multiple Searches
Gather comprehensive information by combining search types:
CODEBLOCK39
Programmatic Processing
Use JSON output for automated processing:
CODEBLOCK40
Building a Knowledge Base
Create searchable documentation from web results:
CODEBLOCK41
Resources
scripts/search.py
The main search tool implementing DuckDuckGo search functionality. Key features:
- - Multiple search types - Web, news, images, and videos
- Flexible filtering - Time range, region, safe search, and type-specific filters
- Multiple output formats - Text, Markdown, and JSON
- File output - Save results for later processing
- Clean formatting - Human-readable output with all essential information
- Error handling - Graceful handling of network errors and empty results
The script can be executed directly and includes comprehensive command-line help via --help.
Web搜索
概述
使用DuckDuckGo的API搜索网页,查找网页、新闻文章、图片和视频中的信息。支持多种格式(文本、Markdown、JSON)返回结果,并提供时间范围、地区和安全搜索等过滤选项。
何时使用此技能
当用户请求以下内容时使用此技能:
- - 搜索信息或资源的网页搜索
- 查找当前或最近的信息
- 查找特定主题的新闻文章
- 按描述或主题搜索图片
- 查找特定主题的视频
- 需要当前网络数据的研究
- 使用网络来源进行事实核查或验证
- 收集某个主题的URL和资源
前提条件
安装所需的依赖:
bash
pip install duckduckgo-search
该库提供了DuckDuckGo搜索API的简单Python接口,无需API密钥或身份验证。
核心功能
1. 基础网页搜索
搜索网页和信息:
bash
python scripts/search.py <查询词>
示例:
bash
python scripts/search.py python asyncio 教程
以清晰的文本格式返回前10个网页结果,包含标题、URL和描述。
2. 限制结果数量
控制返回的结果数量:
bash
python scripts/search.py <查询词> --max-results
示例:
bash
python scripts/search.py 机器学习框架 --max-results 20
适用于:
- - 获取更全面的结果(增加限制)
- 快速查找,结果较少(减少限制)
- 平衡详细程度与处理时间
3. 时间范围过滤
按时效性过滤结果:
bash
python scripts/search.py <查询词> --time-range
时间范围选项:
- - d - 过去一天
- w - 过去一周
- m - 过去一个月
- y - 过去一年
示例:
bash
python scripts/search.py 人工智能新闻 --time-range w
适用于:
4. 新闻搜索
专门搜索新闻文章:
bash
python scripts/search.py <查询词> --type news
示例:
bash
python scripts/search.py 气候变化 --type news --time-range w --max-results 15
新闻结果包含:
- - 文章标题
- 来源出版物
- 发布日期
- URL
- 文章摘要/描述
5. 图片搜索
搜索图片:
bash
python scripts/search.py <查询词> --type images
示例:
bash
python scripts/search.py 日落山景 --type images --max-results 20
图片过滤选项:
尺寸过滤:
bash
python scripts/search.py 风景照片 --type images --image-size Large
选项:Small、Medium、Large、Wallpaper
颜色过滤:
bash
python scripts/search.py 抽象艺术 --type images --image-color Blue
选项:color、Monochrome、Red、Orange、Yellow、Green、Blue、Purple、Pink、Brown、Black、Gray、Teal、White
类型过滤:
bash
python scripts/search.py 图标 --type images --image-type transparent
选项:photo、clipart、gif、transparent、line
布局过滤:
bash
python scripts/search.py 壁纸 --type images --image-layout Wide
选项:Square、Tall、Wide
图片结果包含:
- - 图片标题
- 图片URL(图片直接链接)
- 缩略图URL
- 来源网站
- 尺寸(宽 x 高)
6. 视频搜索
搜索视频:
bash
python scripts/search.py <查询词> --type videos
示例:
bash
python scripts/search.py python教程 --type videos --max-results 15
视频过滤选项:
时长过滤:
bash
python scripts/search.py 烹饪食谱 --type videos --video-duration short
选项:short、medium、long
分辨率过滤:
bash
python scripts/search.py 纪录片 --type videos --video-resolution high
选项:high、standard
视频结果包含:
- - 视频标题
- 发布者/频道
- 时长
- 发布日期
- 视频URL
- 描述
7. 地区特定搜索
按地区搜索结果:
bash
python scripts/search.py <查询词> --region <地区代码>
常用地区代码:
- - us-en - 美国(英语)
- uk-en - 英国(英语)
- ca-en - 加拿大(英语)
- au-en - 澳大利亚(英语)
- de-de - 德国(德语)
- fr-fr - 法国(法语)
- wt-wt - 全球(默认)
示例:
bash
python scripts/search.py 本地新闻 --region us-en --type news
8. 安全搜索控制
控制安全搜索过滤:
bash
python scripts/search.py <查询词> --safe-search
选项:
- - on - 严格过滤
- moderate - 平衡过滤(默认)
- off - 不过滤
示例:
bash
python scripts/search.py 医疗信息 --safe-search on
9. 输出格式
选择结果的格式:
文本格式(默认):
bash
python scripts/search.py 量子计算
清晰可读的纯文本,带编号结果。
Markdown格式:
bash
python scripts/search.py 量子计算 --format markdown
格式化的Markdown,包含标题、粗体文本和链接。
JSON格式:
bash
python scripts/search.py 量子计算 --format json
结构化的JSON数据,适合程序化处理。
10. 保存结果到文件
将搜索结果保存到文件:
bash
python scripts/search.py <查询词> --output <文件路径>
示例:
bash
python scripts/search.py 人工智能 --output ai_results.txt
python scripts/search.py AI新闻 --type news --format markdown --output ai_news.md
python scripts/search.py AI研究 --format json --output ai_data.json
文件格式由--format标志决定,而非文件扩展名。
输出格式示例
文本格式
- 1. 页面标题
URL: https://example.com/page
页面内容的简要描述...
- 2. 另一个结果
URL: https://example.com/another
另一个描述...
Markdown格式
markdown
1. 页面标题
URL: https://example.com/page
页面内容的简要描述...
2. 另一个结果
URL: https://example.com/another
另一个描述...
JSON格式
json
[
{
title: 页面标题,
href: https://example.com/page,
body: 页面内容的简要描述...
},
{
title: 另一个结果,
href: https://example.com/another,
body: 另一个描述...
}
]
常见使用模式
主题研究
收集关于某个主题的全面信息:
bash
从网页获取概述
python scripts/search.py 机器学习基础 --max-results 15 --output ml_web.txt
获取最新新闻
python scripts/search.py 机器学习 --type news --time-range m --output ml_news.txt
查找教程视频
python scripts/search.py 机器学习教程 --type videos --max-results 10 --output ml_videos.txt
时事监控
追踪特定主题的新闻:
bash
python scripts/search.py 气候峰会 --type news --time-range d --format markdown --output dailyclimatenews.md
查找视觉资源
按特定条件搜索图片:
bash
python scripts/search.py 数据可视化示例 --type images --image-type photo --image-size Large --max-results 25 --output viz_images.txt
事实核查
使用最新来源验证信息:
bash
python scripts/search.py 需要验证的具体声明 --time-range w --max-results 20
学术研究
查找学术主题的资源:
bash
python scripts/search.py 量子纠缠研究 --time-range y --max-results 30 --output quantum_research.txt
市场研究
收集产品或公司的信息:
bash
python scripts/search.py 电动汽车市场2025 --max-results 20 --format markdown --output ev_market.md
python scripts/search.py EV新闻 --type news --time-range m --output ev_news.txt
实现方法