web-search

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

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.
通过DuckDuckGo的API搜索网页,查找网页、新闻文章、图片和视频中的信息。支持按时间范围、地区和安全搜索进行过滤,并以多种格式(文本、Markdown、JSON)返回结果。

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
当用户有以下需求时,可使用该Skill:
  • 搜索网页信息或资源
  • 查找线上最新资讯
  • 查阅特定主题的新闻文章
  • 按描述或主题搜索图片
  • 查找特定主题的视频
  • 需要当前网页数据的研究工作
  • 利用网络来源进行事实核查或验证
  • 收集特定主题的URL和资源

Prerequisites

前置条件

Install the required dependency:
bash
pip install duckduckgo-search
This library provides a simple Python interface to DuckDuckGo's search API without requiring API keys or authentication.
安装所需依赖:
bash
pip install duckduckgo-search
该库提供了对接DuckDuckGo搜索API的简易Python接口,无需API密钥或身份验证。

Core Capabilities

核心功能

1. Basic Web Search

1. 基础网页搜索

Search for web pages and information:
bash
python scripts/search.py "<query>"
Example:
bash
python scripts/search.py "python asyncio tutorial"
Returns the top 10 web results with titles, URLs, and descriptions in a clean text format.
搜索网页及相关信息:
bash
python scripts/search.py "<query>"
示例:
bash
python scripts/search.py "python asyncio tutorial"
返回前10条网页结果,包含标题、URL和描述,格式为简洁的文本。

2. Limiting Results

2. 结果数量限制

Control the number of results returned:
bash
python scripts/search.py "<query>" --max-results <N>
Example:
bash
python scripts/search.py "machine learning frameworks" --max-results 20
Useful for:
  • Getting more comprehensive results (increase limit)
  • Quick lookups with fewer results (decrease limit)
  • Balancing detail vs. processing time
控制返回的结果数量:
bash
python scripts/search.py "<query>" --max-results <N>
示例:
bash
python scripts/search.py "machine learning frameworks" --max-results 20
适用场景:
  • 获取更全面的结果(提高限制数)
  • 快速查询少量结果(降低限制数)
  • 平衡结果细节与处理时间

3. Time Range Filtering

3. 时间范围过滤

Filter results by recency:
bash
python scripts/search.py "<query>" --time-range <d|w|m|y>
Time range options:
  • d
    - Past day
  • w
    - Past week
  • m
    - Past month
  • y
    - Past year
Example:
bash
python scripts/search.py "artificial intelligence news" --time-range w
Great for:
  • Finding recent news or updates
  • Filtering out outdated content
  • Tracking recent developments
按时效性过滤结果:
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

4. 新闻搜索

Search specifically for news articles:
bash
python scripts/search.py "<query>" --type news
Example:
bash
python scripts/search.py "climate change" --type news --time-range w --max-results 15
News results include:
  • Article title
  • Source publication
  • Publication date
  • URL
  • Article summary/description
专门搜索新闻文章:
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

5. 图片搜索

Search for images:
bash
python scripts/search.py "<query>" --type images
Example:
bash
python scripts/search.py "sunset over mountains" --type images --max-results 20
Image filtering options:
Size filters:
bash
python scripts/search.py "landscape photos" --type images --image-size Large
Options:
Small
,
Medium
,
Large
,
Wallpaper
Color filters:
bash
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
,
White
Type filters:
bash
python scripts/search.py "icons" --type images --image-type transparent
Options:
photo
,
clipart
,
gif
,
transparent
,
line
Layout filters:
bash
python scripts/search.py "wallpapers" --type images --image-layout Wide
Options:
Square
,
Tall
,
Wide
Image results include:
  • Image title
  • Image URL (direct link to image)
  • Thumbnail URL
  • Source website
  • Dimensions (width x height)
搜索图片:
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
  • 来源网站
  • 尺寸(宽×高)

6. Video Search

6. 视频搜索

Search for videos:
bash
python scripts/search.py "<query>" --type videos
Example:
bash
python scripts/search.py "python tutorial" --type videos --max-results 15
Video filtering options:
Duration filters:
bash
python scripts/search.py "cooking recipes" --type videos --video-duration short
Options:
short
,
medium
,
long
Resolution filters:
bash
python scripts/search.py "documentary" --type videos --video-resolution high
Options:
high
,
standard
Video results include:
  • Video title
  • Publisher/channel
  • Duration
  • Publication date
  • Video URL
  • Description
搜索视频:
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

7. 地区专属搜索

Search with region-specific results:
bash
python scripts/search.py "<query>" --region <region-code>
Common region codes:
  • us-en
    - United States (English)
  • uk-en
    - United Kingdom (English)
  • ca-en
    - Canada (English)
  • au-en
    - Australia (English)
  • de-de
    - Germany (German)
  • fr-fr
    - France (French)
  • wt-wt
    - Worldwide (default)
Example:
bash
python scripts/search.py "local news" --region us-en --type news
搜索特定地区的结果:
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

8. 安全搜索控制

Control safe search filtering:
bash
python scripts/search.py "<query>" --safe-search <on|moderate|off>
Options:
  • on
    - Strict filtering
  • moderate
    - Balanced filtering (default)
  • off
    - No filtering
Example:
bash
python scripts/search.py "medical information" --safe-search on
控制安全搜索过滤级别:
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

9. 输出格式

Choose how results are formatted:
Text format (default):
bash
python scripts/search.py "quantum computing"
Clean, readable plain text with numbered results.
Markdown format:
bash
python scripts/search.py "quantum computing" --format markdown
Formatted markdown with headers, bold text, and links.
JSON format:
bash
python scripts/search.py "quantum computing" --format json
Structured JSON data for programmatic processing.
选择结果的格式:
文本格式(默认):
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

10. 保存结果到文件

Save search results to a file:
bash
python scripts/search.py "<query>" --output <file-path>
Example:
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
The file format is determined by the
--format
flag, not the file extension.
将搜索结果保存到文件:
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...
1. 页面标题
   URL: https://example.com/page
   页面内容的简要描述...

2. 另一结果
   URL: https://example.com/another
   另一描述...

Markdown Format

Markdown格式

markdown
undefined
markdown
undefined

1. Page Title Here

1. 页面标题

Brief description of the page content...
页面内容的简要描述...

2. Another Result

2. 另一结果

Another description...
undefined
另一描述...
undefined

JSON Format

JSON格式

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..."
  }
]
json
[
  {
    "title": "页面标题",
    "href": "https://example.com/page",
    "body": "页面内容的简要描述..."
  },
  {
    "title": "另一结果",
    "href": "https://example.com/another",
    "body": "另一描述..."
  }
]

Common Usage Patterns

常见使用场景

Research on a Topic

主题研究

Gather comprehensive information about a subject:
bash
undefined
收集某一主题的全面信息:
bash
undefined

Get overview from web

获取网页概述

python scripts/search.py "machine learning basics" --max-results 15 --output ml_web.txt
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
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
undefined
python scripts/search.py "machine learning tutorial" --type videos --max-results 10 --output ml_videos.txt
undefined

Current Events Monitoring

时事监控

Track news on specific topics:
bash
python scripts/search.py "climate summit" --type news --time-range d --format markdown --output daily_climate_news.md
追踪特定主题的新闻:
bash
python scripts/search.py "climate summit" --type news --time-range d --format markdown --output daily_climate_news.md

Finding Visual Resources

查找视觉资源

Search for images with specific criteria:
bash
python scripts/search.py "data visualization examples" --type images --image-type photo --image-size Large --max-results 25 --output viz_images.txt
按特定条件搜索图片:
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

事实核查

Verify information with recent sources:
bash
python scripts/search.py "specific claim to verify" --time-range w --max-results 20
通过近期来源验证信息:
bash
python scripts/search.py "specific claim to verify" --time-range w --max-results 20

Academic Research

学术研究

Find resources on scholarly topics:
bash
python scripts/search.py "quantum entanglement research" --time-range y --max-results 30 --output quantum_research.txt
查找学术主题的资源:
bash
python scripts/search.py "quantum entanglement research" --time-range y --max-results 30 --output quantum_research.txt

Market Research

市场调研

Gather information about products or companies:
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
收集产品或公司相关信息:
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

实现流程

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
当用户请求网页搜索时:
  1. 识别搜索意图
    • 需要哪种类型的内容(网页、新闻、图片、视频)?
    • 结果需要多新?
    • 需要多少条结果?
    • 有哪些过滤需求?
  2. 配置搜索参数
    • 选择合适的搜索类型(
      --type
    • 若关注时效性,设置时间范围(
      --time-range
    • 调整结果数量(
      --max-results
    • 应用过滤条件(图片尺寸、视频时长等)
  3. 选择输出格式
    • 快速阅读选文本格式
    • 文档编写选Markdown格式
    • 后续处理选JSON格式
  4. 执行搜索
    • 运行搜索命令
    • 若需保存结果,使用输出文件参数
    • 直接打印到标准输出以便即时查看
  5. 处理结果
    • 若有需要,读取保存的文件
    • 提取URL或特定信息
    • 合并多次搜索的结果

Quick Reference

快速参考

Command structure:
bash
python scripts/search.py "<query>" [options]
Essential options:
  • -t, --type
    - Search type (web, news, images, videos)
  • -n, --max-results
    - Maximum results (default: 10)
  • --time-range
    - Time filter (d, w, m, y)
  • -r, --region
    - Region code (e.g., us-en, uk-en)
  • --safe-search
    - Safe search level (on, moderate, off)
  • -f, --format
    - Output format (text, markdown, json)
  • -o, --output
    - Save to file
Image-specific options:
  • --image-size
    - Size filter (Small, Medium, Large, Wallpaper)
  • --image-color
    - Color filter
  • --image-type
    - Type filter (photo, clipart, gif, transparent, line)
  • --image-layout
    - Layout filter (Square, Tall, Wide)
Video-specific options:
  • --video-duration
    - Duration filter (short, medium, long)
  • --video-resolution
    - Resolution filter (high, standard)
Get full help:
bash
python scripts/search.py --help
命令结构:
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. Be specific - Use clear, specific search queries for better results
  2. Use time filters - Apply
    --time-range
    for current information
  3. Adjust result count - Start with 10-20 results, increase if needed
  4. Save important searches - Use
    --output
    to preserve results
  5. Choose appropriate type - Use news search for current events, web for general info
  6. Use JSON for automation - JSON format is easiest to parse programmatically
  7. Respect usage - Don't hammer the API with rapid repeated searches
  1. 查询具体 - 使用清晰、具体的搜索词以获得更优结果
  2. 使用时间过滤 - 若需最新信息,应用
    --time-range
    参数
  3. 调整结果数量 - 先获取10-20条结果,必要时再增加
  4. 保存重要搜索 - 使用
    --output
    参数保存结果
  5. 选择合适类型 - 时事用新闻搜索,通用信息用网页搜索
  6. 自动化用JSON - JSON格式最便于程序化解析
  7. 合理使用 - 避免频繁连续调用API

Troubleshooting

故障排除

Common issues:
  • "Missing required dependency": Run
    pip install duckduckgo-search
  • 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
常见问题:
  • "Missing required dependency":运行
    pip install duckduckgo-search
  • 无结果返回:尝试更宽泛的搜索词或移除时间过滤
  • 超时错误:搜索服务可能暂时不可用,稍后重试
  • 速率限制:若进行大量请求,需间隔搜索时间
  • 结果不符合预期:DuckDuckGo的结果可能与Google不同,尝试优化查询词
局限性:
  • 结果质量取决于DuckDuckGo的索引和算法
  • 不支持高级搜索运算符(如Google的site:, filetype:等)
  • 图片和视频搜索的结果数量可能少于网页搜索
  • 无法控制结果排序或相关性评分
  • 某些专业搜索在专用搜索引擎上效果更好

Advanced Use Cases

高级使用场景

Combining Multiple Searches

合并多次搜索

Gather comprehensive information by combining search types:
bash
undefined
通过合并不同类型的搜索收集全面信息:
bash
undefined

Web overview

网页概述

python scripts/search.py "topic" --max-results 15 --output topic_web.txt
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
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
undefined
python scripts/search.py "topic" --type images --max-results 20 --output topic_images.txt
undefined

Programmatic Processing

程序化处理

Use JSON output for automated processing:
bash
python scripts/search.py "research topic" --format json --output results.json
使用JSON输出进行自动化处理:
bash
python scripts/search.py "research topic" --format json --output results.json

Then process with another script

随后用其他脚本处理

python analyze_results.py results.json
undefined
python analyze_results.py results.json
undefined

Building a Knowledge Base

构建知识库

Create searchable documentation from web results:
bash
undefined
从网页结果创建可搜索的文档:
bash
undefined

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
undefined
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
undefined

Resources

资源

scripts/search.py

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
.
实现DuckDuckGo搜索功能的核心工具。主要特性:
  • 多种搜索类型 - 网页、新闻、图片和视频
  • 灵活过滤 - 时间范围、地区、安全搜索及专属类型过滤
  • 多种输出格式 - 文本、Markdown和JSON
  • 文件输出 - 保存结果供后续处理
  • 简洁格式化 - 包含所有关键信息的易读输出
  • 错误处理 - 优雅处理网络错误和空结果
该脚本可直接执行,通过
--help
参数可查看完整的命令行帮助。