Skip to main content

Search for words, documents, images, news, maps and text translation using the DuckDuckGo.com search engine.

Project description

Python >= 3.8 Downloads Downloads

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.

Table of Contents

Install

pip install -U duckduckgo_search

There is also a beta release that uses the httpx library:

pip install -U duckduckgo_search==5.3.1b1

[!NOTE] you can install lxml to use the text function with backend='html' or backend='lite' (size ≈ 12Mb)
pip install -U duckduckgo_search[lxml]

CLI version

ddgs --help

or

python -m duckduckgo_search --help

CLI examples:

# AI chat
ddgs chat
# text search
ddgs text -k "'how to tame a fox' site:wikihow.com"
# find and download pdf files via proxy (example: Tor browser)
ddgs text -k "pushkin filetype:pdf" -r wt-wt -m 50 -d -p socks5://127.0.0.1:9150
# find and save to csv
ddgs text -k "neuroscience exploring the brain filetype:pdf" -m 70 -o csv
# find and download images
ddgs images -k "beware of false prophets" -r wt-wt -type photo -m 500 -d
# get news for the last day and save to json
ddgs news -k "ukraine war" -m 50 -t d -o json

Go To TOP

Duckduckgo search operators

Keywords example Result
cats dogs Results about cats or dogs
"cats and dogs" Results for exact term "cats and dogs". If no results are found, related results are shown.
cats -dogs Fewer dogs in results
cats +dogs More dogs in results
cats filetype:pdf PDFs about cats. Supported file types: pdf, doc(x), xls(x), ppt(x), html
dogs site:example.com Pages about dogs from example.com
cats -site:example.com Pages about cats, excluding example.com
intitle:dogs Page title includes the word "dogs"
inurl:cats Page url includes the word "cats"

Go To TOP

Regions

expand
xa-ar for Arabia
xa-en for Arabia (en)
ar-es for Argentina
au-en for Australia
at-de for Austria
be-fr for Belgium (fr)
be-nl for Belgium (nl)
br-pt for Brazil
bg-bg for Bulgaria
ca-en for Canada
ca-fr for Canada (fr)
ct-ca for Catalan
cl-es for Chile
cn-zh for China
co-es for Colombia
hr-hr for Croatia
cz-cs for Czech Republic
dk-da for Denmark
ee-et for Estonia
fi-fi for Finland
fr-fr for France
de-de for Germany
gr-el for Greece
hk-tzh for Hong Kong
hu-hu for Hungary
in-en for India
id-id for Indonesia
id-en for Indonesia (en)
ie-en for Ireland
il-he for Israel
it-it for Italy
jp-jp for Japan
kr-kr for Korea
lv-lv for Latvia
lt-lt for Lithuania
xl-es for Latin America
my-ms for Malaysia
my-en for Malaysia (en)
mx-es for Mexico
nl-nl for Netherlands
nz-en for New Zealand
no-no for Norway
pe-es for Peru
ph-en for Philippines
ph-tl for Philippines (tl)
pl-pl for Poland
pt-pt for Portugal
ro-ro for Romania
ru-ru for Russia
sg-en for Singapore
sk-sk for Slovak Republic
sl-sl for Slovenia
za-en for South Africa
es-es for Spain
se-sv for Sweden
ch-de for Switzerland (de)
ch-fr for Switzerland (fr)
ch-it for Switzerland (it)
tw-tzh for Taiwan
th-th for Thailand
tr-tr for Turkey
ua-uk for Ukraine
uk-en for United Kingdom
us-en for United States
ue-es for United States (es)
ve-es for Venezuela
vn-vi for Vietnam
wt-wt for No region

Go To TOP

DDGS and AsyncDDGS classes

The DDGS and AsyncDDGS classes are used to retrieve search results from DuckDuckGo.com. To use the AsyncDDGS class, you can perform asynchronous operations using Python's asyncio library. To initialize an instance of the DDGS or AsyncDDGS classes, you can provide the following optional arguments:

class DDGS:
    """DuckDuckgo_search class to get search results from duckduckgo.com

    Args:
        headers (dict, optional): Dictionary of headers for the HTTP client. Defaults to None.
        proxy (str, optional): proxy for the HTTP client, supports http/https/socks5 protocols.
            example: "http://user:pass@example.com:3128". Defaults to None.
        timeout (int, optional): Timeout value for the HTTP client. Defaults to 10.
    """

Here is an example of initializing the DDGS class.

from duckduckgo_search import DDGS

results = DDGS().text("python programming", max_results=5)
print(results)

Here is an example of initializing the AsyncDDGS class:

import asyncio

from duckduckgo_search import AsyncDDGS

async def aget_results(word):
    results = await AsyncDDGS(proxy=None).atext(word, max_results=100)
    return results

async def main():
    words = ["sun", "earth", "moon"]
    tasks = [aget_results(w) for w in words]
    results = await asyncio.gather(*tasks)
    print(results)

if __name__ == "__main__":
    asyncio.run(main())

Go To TOP

Proxy

Package supports http/https/socks proxies. Example: http://user:pass@example.com:3128. Use a rotating proxy. Otherwise, use a new proxy with each DDGS or AsyncDDGS initialization.

1. The easiest way. Launch the Tor Browser

ddgs = DDGS(proxy="socks5://127.0.0.1:9150", timeout=20)
results = ddgs.text("something you need", max_results=50)

2. Use any proxy server (example with iproyal rotating residential proxies)

ddgs = DDGS(proxy="socks5://user:password@geo.iproyal.com:32325", timeout=20)
results = ddgs.text("something you need", max_results=50)

Go To TOP

Exceptions

Exceptions:

  • DuckDuckGoSearchException: Base exception for duckduckgo_search errors.
  • RatelimitException: Inherits from DuckDuckGoSearchException, raised for exceeding API request rate limits.
  • TimeoutException: Inherits from DuckDuckGoSearchException, raised for API request timeouts.

Go To TOP

1. chat() - AI chat

def chat(self, keywords: str, model: str = "gpt-3.5") -> str:
    """Initiates a chat session with DuckDuckGo AI.

    Args:
        keywords (str): The initial message or question to send to the AI.
        model (str): The model to use: "gpt-3.5", "claude-3-haiku". Defaults to "gpt-3.5".

    Returns:
        str: The response from the AI.
    """

Example

results = DDGS().chat("summarize Daniel Defoe's The Consolidator", model='claude-3-haiku')

# async
results = await AsyncDDGS().achat('describe the characteristic habits and behaviors of humans as a species')

Go To TOP

2. text() - text search by duckduckgo.com

def text(
    keywords: str,
    region: str = "wt-wt",
    safesearch: str = "moderate",
    timelimit: Optional[str] = None,
    backend: str = "api",
    max_results: Optional[int] = None,
) -> List[Dict[str, str]]:
    """DuckDuckGo text search generator. Query params: https://duckduckgo.com/params.

    Args:
        keywords: keywords for query.
        region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".
        safesearch: on, moderate, off. Defaults to "moderate".
        timelimit: d, w, m, y. Defaults to None.
        backend: api, html, lite. Defaults to api.
            api - collect data from https://duckduckgo.com,
            html - collect data from https://html.duckduckgo.com,
            lite - collect data from https://lite.duckduckgo.com.
        max_results: max number of results. If None, returns results only from the first response. Defaults to None.

    Returns:
        List of dictionaries with search results.
    """

Example

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().atext('sun', region='wt-wt', safesearch='off', timelimit='y', max_results=10)

Go To TOP

3. answers() - instant answers by duckduckgo.com

def answers(keywords: str) -> List[Dict[str, str]]:
    """DuckDuckGo instant answers. Query params: https://duckduckgo.com/params.
    
    Args:
        keywords: keywords for query,
    
    Returns:
        List of dictionaries with instant answers results.
    """

Example

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

# async
results = await AsyncDDGS().aanswers("sun")

Go To TOP

4. images() - image search by duckduckgo.com

def images(
    keywords: str,
    region: str = "wt-wt",
    safesearch: str = "moderate",
    timelimit: Optional[str] = None,
    size: Optional[str] = None,
    color: Optional[str] = None,
    type_image: Optional[str] = None,
    layout: Optional[str] = None,
    license_image: Optional[str] = None,
    max_results: Optional[int] = None,
) -> List[Dict[str, str]]:
    """DuckDuckGo images search. Query params: https://duckduckgo.com/params.
    
    Args:
        keywords: keywords for query.
        region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".
        safesearch: on, moderate, off. Defaults to "moderate".
        timelimit: Day, Week, Month, Year. Defaults to None.
        size: Small, Medium, Large, Wallpaper. Defaults to None.
        color: color, Monochrome, Red, Orange, Yellow, Green, Blue,
            Purple, Pink, Brown, Black, Gray, Teal, White. Defaults to None.
        type_image: photo, clipart, gif, transparent, line.
            Defaults to None.
        layout: Square, Tall, Wide. Defaults to None.
        license_image: any (All Creative Commons), Public (PublicDomain),
            Share (Free to Share and Use), ShareCommercially (Free to Share and Use Commercially),
            Modify (Free to Modify, Share, and Use), ModifyCommercially (Free to Modify, Share, and
            Use Commercially). Defaults to None.
        max_results: max number of results. If None, returns results only from the first response. Defaults to None.
    
    Returns:
        List of dictionaries with images search results.
    """

Example

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().aimages('sun', region='wt-wt', safesearch='off', max_results=20)

Go To TOP

5. videos() - video search by duckduckgo.com

def videos(
    keywords: str,
    region: str = "wt-wt",
    safesearch: str = "moderate",
    timelimit: Optional[str] = None,
    resolution: Optional[str] = None,
    duration: Optional[str] = None,
    license_videos: Optional[str] = None,
    max_results: Optional[int] = None,
) -> List[Dict[str, str]]:
    """DuckDuckGo videos search. Query params: https://duckduckgo.com/params.
    
    Args:
        keywords: keywords for query.
        region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".
        safesearch: on, moderate, off. Defaults to "moderate".
        timelimit: d, w, m. Defaults to None.
        resolution: high, standart. Defaults to None.
        duration: short, medium, long. Defaults to None.
        license_videos: creativeCommon, youtube. Defaults to None.
        max_results: max number of results. If None, returns results only from the first response. Defaults to None.
    
    Returns:
        List of dictionaries with videos search results.
    """

Example

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

# async
results = await AsyncDDGS().avideos('sun', region='wt-wt', safesearch='off', timelimit='y', max_results=10)

Go To TOP

6. news() - news search by duckduckgo.com

def news(
    keywords: str,
    region: str = "wt-wt",
    safesearch: str = "moderate",
    timelimit: Optional[str] = None,
    max_results: Optional[int] = None,
) -> List[Dict[str, str]]:
    """DuckDuckGo news search. Query params: https://duckduckgo.com/params.
    
    Args:
        keywords: keywords for query.
        region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".
        safesearch: on, moderate, off. Defaults to "moderate".
        timelimit: d, w, m. Defaults to None.
        max_results: max number of results. If None, returns results only from the first response. Defaults to None.
    
    Returns:
        List of dictionaries with news search results.
    """

Example

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

# async
results = await AsyncDDGS().anews('sun', region='wt-wt', safesearch='off', timelimit='d', max_results=10)

Go To TOP

7. maps() - map search by duckduckgo.com

def maps(
    keywords,
    place: Optional[str] = None,
    street: Optional[str] = None,
    city: Optional[str] = None,
    county: Optional[str] = None,
    state: Optional[str] = None,
    country: Optional[str] = None,
    postalcode: Optional[str] = None,
    latitude: Optional[str] = None,
    longitude: Optional[str] = None,
    radius: int = 0,
    max_results: Optional[int] = None,
) -> List[Dict[str, str]]:
    """DuckDuckGo maps search. Query params: https://duckduckgo.com/params.
    
    Args:
        keywords: keywords for query
        place: if set, the other parameters are not used. Defaults to None.
        street: house number/street. Defaults to None.
        city: city of search. Defaults to None.
        county: county of search. Defaults to None.
        state: state of search. Defaults to None.
        country: country of search. Defaults to None.
        postalcode: postalcode of search. Defaults to None.
        latitude: geographic coordinate (north-south position). Defaults to None.
        longitude: geographic coordinate (east-west position); if latitude and
            longitude are set, the other parameters are not used. Defaults to None.
        radius: expand the search square by the distance in kilometers. Defaults to 0.
        max_results: max number of results. If None, returns results only from the first response. Defaults to None.
    
    Returns:
        List of dictionaries with maps search results.
    """

Example

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

# async
results = await AsyncDDGS().amaps('shop', place="Baltimor", max_results=10)

Go To TOP

8. translate() - translation by duckduckgo.com

def translate(
    self,
    keywords: str,
    from_: Optional[str] = None,
    to: str = "en",
) -> List[Dict[str, str]]:
    """DuckDuckGo translate.
    
    Args:
        keywords: string or list of strings to translate.
        from_: translate from (defaults automatically). Defaults to None.
        to: what language to translate. Defaults to "en".
    
    Returns:
        List od dictionaries with translated keywords.
    """

Example

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

# async
results = await AsyncDDGS().atranslate('sun', to="de")

Go To TOP

9. suggestions() - suggestions by duckduckgo.com

def suggestions(
    keywords,
    region: str = "wt-wt",
) -> List[Dict[str, str]]:
    """DuckDuckGo suggestions. Query params: https://duckduckgo.com/params.
    
    Args:
        keywords: keywords for query.
        region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".
    
    Returns:
        List of dictionaries with suggestions results.
    """

Example

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

# async
results = await AsyncDDGS().asuggestions('sun')

Go To TOP

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

duckduckgo_search-6.1.2.tar.gz (26.9 kB view details)

Uploaded Source

Built Distribution

duckduckgo_search-6.1.2-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file duckduckgo_search-6.1.2.tar.gz.

File metadata

  • Download URL: duckduckgo_search-6.1.2.tar.gz
  • Upload date:
  • Size: 26.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for duckduckgo_search-6.1.2.tar.gz
Algorithm Hash digest
SHA256 4250c937db9b9c6831e6960455c7fc6cc86825764b0020c0edd2c302db5bc2f7
MD5 01b61488d1fc59806e9140af2b39e799
BLAKE2b-256 4ba97a5556f9455b20ed1799c9af6f89e5b61423cea21c490161f20f3c772b12

See more details on using hashes here.

File details

Details for the file duckduckgo_search-6.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for duckduckgo_search-6.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 74b04a4c7e0803c1b93ea51dcfdc92fce26d60757c76ba897ed0c424297d27f2
MD5 b7250d574489d40bb1cee1cbb192d4b0
BLAKE2b-256 1b4d47c68ff88ef131490b55438dc8294bdf3adf14a97c2cd9576a0d4c41c8bd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page