当前位置:   article > 正文

duckduckgo-search_duckduckgo_search

duckduckgo_search


关于 DuckDuckGo

DuckDuckGo 创立于 2008 年,创始人是毕业于麻省理工的 Gabriel Weinberg。谈到这个奇怪的网站名字,Weinberg 表示是受启发于儿童玩的追逐游戏「duck, duck, goose」,并没有其他深意或隐喻。


支持平台:


一些文章


DuckDuckGo 也支持通过在 URL 上附加参数(parameter)的方式进行高级搜索。例如,下面的 URL:

https://duckduckgo.com/?q=wikipedia&kp=0&kl=cn-zh
  • 1

将会发起一次关键词为「wikipedia」(q=wikipedia)、不过滤搜索结果(kp=0)、地区范围为中国(kl=cn-zh)的搜索。


与 Google 等具备类似功能的搜索引擎不同的是,DuckDuckGo 不仅支持通过 URL 参数定义搜索的方式,而且支持定义搜索页面的行为(如是否在新标签页中打开链接)和外观(如主题背景)。(可以在 DuckDuckGo 的文档中找到全部可用的参数。)


关于 duckduckgo-search

Search for words, documents, images, videos, news, maps and text translation using the DuckDuckGo.com search engine. Downloading files and images to a local hard drive.


安装

pip install -U duckduckgo_search
  • 1

CLI 命令行使用

$ ddgs --help
Usage: ddgs [OPTIONS] COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...

  A decorator that creates a command group.

  This decorator is used to create a group of commands. The `chain=True`
  parameter allows the commands in the group to be chained together, meaning
  the output of one command can be used as the input for the next command.

Options:
  --help  Show this message and exit.

Commands:
  answers      CLI function to perform a answers search using DuckDuckGo...
  images       CLI function to perform a images search using DuckDuckGo API.
  maps         CLI function to perform a maps search using DuckDuckGo API.
  news         CLI function to perform a news search using DuckDuckGo API.
  suggestions  CLI function to perform a suggestions search using...
  text         CLI function to perform a text search using DuckDuckGo API.
  translate    CLI function to perform translate using DuckDuckGo API.
  version      A command-line interface command that prints and returns...
  videos       CLI function to perform a videos search using DuckDuckGo API.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

或者

python -m duckduckgo_search --help
  • 1

使用示例

# 文字搜索
ddgs text -k "ayrton senna"

# 使用协议搜索文本  (example: Tor Browser)
ddgs text -k "china is a global threat" -p socks5://localhost:9150

# 搜索和下载 pdf 文件
ddgs text -k "russia filetype:pdf" -m 50 -d

# find in es-es region and download pdf files via proxy (example: Tor browser)
ddgs text -k "embajada a tamorlán filetype:pdf" -r es-es -m 50 -d -p socks5://localhost:9150

#  从指定站点搜索和下载 xls 文件
ddgs text -k "sanctions filetype:xls site:gov.ua" -m 50 -d

# 从指定站点搜索和下载任意 doc(x) 文件
ddgs text -k "filetype:doc site:mos.ru" -m 50 -d

# 查找和下载 images
ddgs images -k "yuri kuklachev cat theatre" -m 500 -s off -d

# find in br-br region and download images via proxy (example: Tor browser) in 10 threads
ddgs images -k "rio carnival" -r br-br -s off -m 500 -d -th 10 -p socks5://localhost:9150

# 获取最新的新闻
ddgs news -k "ukraine war" -s off -t d -m 10

# 获取上一天的新闻,并保存到一个 csv 文件
ddgs news -k "hubble telescope" -t d -m 50 -o csv

# 获取答案并保存到一个 json 文件
ddgs answers -k holocaust -o json
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

Duckduckgo search operators

Keywords exampleResult
cats dogsResults about cats or dogs
“cats and dogs”Results for exact term “cats and dogs”. If no results are found, related results are shown.
cats -dogsFewer dogs in results
cats +dogsMore dogs in results
cats filetype:pdfPDFs about cats. Supported file types: pdf, doc(x), xls(x), ppt(x), html
dogs site:example.comPages about dogs from example.com
cats -site:example.comPages about cats, excluding example.com
intitle:dogsPage title includes the word “dogs”
inurl:catsPage url includes the word “cats”

基本使用

1、文本搜索

from duckduckgo_search import DDGS

results = DDGS().text('live free or die', region='wt-wt', safesearch='off', timelimit='y', max_results=10)

# Searching for pdf files
results = DDGS().text('russia filetype:pdf', region='wt-wt', safesearch='off', timelimit='y', max_results=10)

# async
results = await AsyncDDGS().text('sun', region='wt-wt', safesearch='off', timelimit='y', max_results=10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2、搜索即时答案

from duckduckgo_search import DDGS

results = DDGS().answers("sun")

# async
results = await AsyncDDGS().answers("sun")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3、搜索图片

from duckduckgo_search import DDGS

results = DDGS().images(
    keywords="butterfly",
    region="wt-wt",
    safesearch="off",
    size=None,
    color="Monochrome",
    type_image=None,
    layout=None,
    license_image=None,
    max_results=100,
)

# async
results = await AsyncDDGS().images('sun', region='wt-wt', safesearch='off', max_results=20)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

4、搜索视频

from duckduckgo_search import DDGS

results = DDGS().videos(
    keywords="cars",
    region="wt-wt",
    safesearch="off",
    timelimit="w",
    resolution="high",
    duration="medium",
    max_results=100,
)

# async
results = await AsyncDDGS().videos('sun', region='wt-wt', safesearch='off', timelimit='y', max_results=10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

5、搜索新闻

from duckduckgo_search import DDGS

results = DDGS().news(keywords="sun", region="wt-wt", safesearch="off", timelimit="m", max_results=20)

# async
results = await AsyncDDGS().news('sun', region='wt-wt', safesearch='off', timelimit='d', max_results=10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

6、地图搜索

from duckduckgo_search import DDGS

results = DDGS().maps("school", place="Uganda", max_results=50)

# async
results = await AsyncDDGS().maps('shop', place="Baltimor", max_results=10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

7、翻译

from duckduckgo_search import DDGS

keywords = 'school'
# also valid
keywords = ['school', 'cat']
results = DDGS().translate(keywords, to="de")

# async
results = await AsyncDDGS().translate('sun', to="de")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

8、建议

from duckduckgo_search import DDGS

results = DDGS().suggestions("fly")

# async
results = await AsyncDDGS().suggestions('sun')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

伊织 2024-03-20

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/546758
推荐阅读
相关标签
  

闽ICP备14008679号