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.
- Install
- CLI version
- Duckduckgo search operators
- Regions
- DDGS and AsyncDDGS classes
- Proxies
- Exceptions
- 1. text() - text search
- 2. answers() - instant answers
- 3. images() - image search
- 4. videos() - video search
- 5. news() - news search
- 6. maps() - map search
- 7. translate() - translation
- 8. suggestions() - suggestions
pip install -U duckduckgo_search
☝️
duckduckgo_search
also usesorjson
if it is installed.
ddgs --help
or
python -m duckduckgo_search --help
CLI examples:
# text search
ddgs text -k "ayrton senna"
# text search via proxy (example: Tor Browser)
ddgs text -k "han zhongli kills a tiger" -p socks5://localhost:9150
# find and download pdf files
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
# find and download xls files from a specific site
ddgs text -k "sanctions filetype:xls site:gov.ua" -m 50 -d
# find and download any doc(x) files from a specific site
ddgs text -k "filetype:doc site:mos.ru" -m 50 -d
# find and download 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
# get latest news
ddgs news -k "ukraine war" -s off -t d -m 10
# get last day's news and save it to a csv file
ddgs news -k "hubble telescope" -t d -m 50 -o csv
# get answers and save to a json file
ddgs answers -k holocaust -o json
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" |
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
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.
proxies (Union[dict, str], optional): Proxies for the HTTP client (can be dict or str). 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
with DDGS() as ddgs:
results = ddgs.text("python programming", max_results=5)
print(results)
Here is an example of initializing the AsyncDDGS class:
import asyncio
import logging
from duckduckgo_search import AsyncDDGS
async def aget_results(word):
async with AsyncDDGS(proxies=None) as addgs:
results = await addgs.text(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__":
logging.basicConfig(level=logging.DEBUG)
asyncio.run(main())
Proxy can be specified as a dictionary or just a string
proxies = {"http": "socks5://localhost:9150", "https": "socks5://localhost:9150"}
proxies = "socks5://localhost:9150"
1. The easiest way. Launch the Tor Browser
with DDGS(proxies="socks5://localhost:9150", timeout=20) as ddgs:
results = ddgs.text("something you need", max_results=50)
2. Use any proxy server (example with iproyal residential proxies)
with DDGS(proxies="socks5://user:password@geo.iproyal.com:32325", timeout=20) as ddgs:
results = ddgs.text("something you need", max_results=50)s
Exceptions:
DuckDuckGoSearchException
: Raised when there is a generic exception during the API request.
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.
Raises:
DuckDuckGoSearchException: Raised when there is a generic exception during the API request.
"""
Example
with DDGS() as 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
async with AsyncDDGS() as addgs:
results = await addgs.text('sun', region='wt-wt', safesearch='off', timelimit='y', max_results=10)
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.
Raises:
DuckDuckGoSearchException: Raised when there is a generic exception during the API request.
"""
Example
with DDGS() as ddgs:
results = ddgs.answers("sun")
# async
async with AsyncDDGS() as addgs:
results = await addgs.answers("sun")
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.
Raises:
DuckDuckGoSearchException: Raised when there is a generic exception during the API request.
"""
Example
with DDGS() as 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
async with AsyncDDGS() as addgs:
results = await addgs.images('sun', region='wt-wt', safesearch='off', max_results=20)
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.
Raises:
DuckDuckGoSearchException: Raised when there is a generic exception during the API request.
"""
Example
with DDGS() as ddgs:
results = ddgs.videos(
keywords="cars",
region="wt-wt",
safesearch="off",
timelimit="w",
resolution="high",
duration="medium",
max_results=100,
)
# async
async with AsyncDDGS() as addgs:
results = await addgs.videos('sun', region='wt-wt', safesearch='off', timelimit='y', max_results=10)
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.
Raises:
DuckDuckGoSearchException: Raised when there is a generic exception during the API request.
"""
Example
with DDGS() as ddgs:
results = ddgs.news(keywords="sun", region="wt-wt", safesearch="off", timelimit="m", max_results=20)
# async
async with AsyncDDGS() as addgs:
results = await addgs.news('sun', region='wt-wt', safesearch='off', timelimit='d', max_results=10)
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.
Raises:
DuckDuckGoSearchException: Raised when there is a generic exception during the API request.
"""
Example
with DDGS() as ddgs:
results = ddgs.maps("school", place="Uganda", max_results=50)
# async
async with AsyncDDGS() as addgs:
results = await addgs.maps('shop', place="Baltimor", max_results=10)
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.
Raises:
DuckDuckGoSearchException: Raised when there is a generic exception during the API request.
"""
Example
keywords = 'school'
# also valid
keywords = ['school', 'cat']
with DDGS() as ddgs:
results = ddgs.translate(keywords, to="de")
# async
async with AsyncDDGS() as addgs:
results = await addgs.translate('sun', to="de")
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.
Raises:
DuckDuckGoSearchException: Raised when there is a generic exception during the API request.
"""
Example
with DDGS() as ddgs:
results = ddgs.suggestions("fly")
# async
async with AsyncDDGS() as addgs:
results = await addgs.suggestions('sun')