Package: duckduckgo-search
Duckduckgo_searchSearch 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.⚠️ Warning: use AsyncDDGS in asynchronous codeTable of ContentsInstallCLI versionDuckduckgo search operatorsRegionsDDGS and AsyncDDGS classesProxiesExceptions1. text() - text search2. answers() - instant answers3. images() - image search4. videos() - video search5. news() - news search6. maps() - map search7. translate() - translation8. suggestions() - suggestionsInstallpipinstall-Uduckduckgo_searchCLI versionddgs--helporpython-mduckduckgo_search--helpCLI examples:# text searchddgstext-k"ayrton senna"# text search via proxy (example: Tor Browser)ddgstext-k"china is a global threat"-psocks5://localhost:9150# find and download pdf filesddgstext-k"russia filetype:pdf"-m50-d# find in es-es region and download pdf files via proxy (example: Tor browser)ddgstext-k"embajada a tamorlán filetype:pdf"-res-es-m50-d-psocks5://localhost:9150# find and download xls files from a specific siteddgstext-k"sanctions filetype:xls site:gov.ua"-m50-d# find and download any doc(x) files from a specific siteddgstext-k"filetype:doc site:mos.ru"-m50-d# find and download imagesddgsimages-k"yuri kuklachev cat theatre"-m500-soff-d# find in br-br region and download images via proxy (example: Tor browser) in 10 threadsddgsimages-k"rio carnival"-rbr-br-soff-m500-d-th10-psocks5://localhost:9150# get latest newsddgsnews-k"ukraine war"-soff-td-m10# get last day's news and save it to a csv fileddgsnews-k"hubble telescope"-td-m50-ocsv# get answers and save to a json fileddgsanswers-kholocaust-ojsonGo To TOPDuckduckgo search operatorsKeywords exampleResultcats 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 resultscats +dogsMore dogs in resultscats filetype:pdfPDFs about cats. Supported file types: pdf, doc(x), xls(x), ppt(x), htmldogs site:example.comPages about dogs from example.comcats -site:example.comPages about cats, excluding example.comintitle:dogsPage title includes the word "dogs"inurl:catsPage url includes the word "cats"Go To TOPRegionsexpandxa-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 regionGo To TOPDDGS and AsyncDDGS classesThe 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:classDDGS:"""DuckDuckgo_search class to get search results from duckduckgo.comArgs: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:fromduckduckgo_searchimportDDGSwithDDGS()asddgs:results=[rforrinddgs.text("python programming",max_results=5)]print(results)Here is an example of initializing the AsyncDDGS class:importasyncioimportloggingimportsysfromitertoolsimportchainfromrandomimportshuffleimportrequestsfromduckduckgo_searchimportAsyncDDGS# bypass curl-cffi NotImplementedError in windows https://curl-cffi.readthedocs.io/en/latest/faq/ifsys.platform.lower().startswith("win"):asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())defget_words():word_site="https://www.mit.edu/~ecprice/wordlist.10000"resp=requests.get(word_site)words=resp.text.splitlines()returnwordsasyncdefaget_results(word):asyncwithAsyncDDGS(proxies=proxies)asddgs:results=[rasyncforrinddgs.text(word,max_results=None)]returnresultsasyncdefmain():words=get_words()shuffle(words)tasks=[]forwordinwords[:10]:tasks.append(aget_results(word))results=awaitasyncio.gather(*tasks)print(f"Done")forrinchain.from_iterable(results):print(r)if__name__=="__main__":logging.basicConfig(level=logging.DEBUG)asyncio.run(main())⚠️ Always use DDGS and AsyncDDGS as a context manager (with DDGS() as ddgs:orasync with AsyncDDGS as addgs:).Go To TOPProxiesProxy can be specified as a dictionary or just a stringproxies={"http":"socks5://localhost:9150","https":"socks5://localhost:9150"}proxies="socks5://localhost:9150"1. The easiest way. Launch the Tor Browserfromduckduckgo_searchimportDDGSwithDDGS(proxies="socks5://localhost:9150",timeout=20)asddgs:forrinddgs.text("something you need",max_results=50):print(r)2. Use any proxy server(example withiproyal residential proxies)fromduckduckgo_searchimportDDGSwithDDGS(proxies="socks5://user:password@geo.iproyal.com:32325",timeout=20)asddgs:forrinddgs.text("something you need",max_results=50):print(r)Go To TOPExceptionsExceptions:DuckDuckGoSearchException: Raised when there is a generic exception during the API request.Go To TOP1. text() - text search by duckduckgo.comdeftext(keywords:str,region:str="wt-wt",safesearch:str="moderate",timelimit:Optional[str]=None,backend:str="api",max_results:Optional[int]=None,)->Generator[Dict[str,Optional[str]],None,None]:"""DuckDuckGo text search generator. Query params: https://duckduckgo.com/paramsArgs: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.Yields:dict with search results."""Examplefromduckduckgo_searchimportDDGSwithDDGS()asddgs:results=[rforrinddgs.text('live free or die',region='wt-wt',safesearch='off',timelimit='y',max_results=10)]# Searching for pdf fileswithDDGS()asddgs:results=[forrinddgs.text('russia filetype:pdf',region='wt-wt',safesearch='off',timelimit='y',max_results=10)]# asyncwithAsyncDDGS()asaddgs:results=[rasyncforrinaddgs.text('sun',region='wt-wt',safesearch='off',timelimit='y',max_results=10)]Go To TOP2. answers() - instant answers by duckduckgo.comdefanswers(keywords:str)->Generator[Dict[str,Optional[str]],None,None]:"""DuckDuckGo instant answers. Query params: https://duckduckgo.com/paramsArgs:keywords: keywords for query.Yields:dict with instant answers results."""Examplefromduckduckgo_searchimportDDGSwithDDGS()asddgs:results=[rforrinddgs.answers("sun")]# asyncwithAsyncDDGS()asaddgs:results=[rasyncforrinaddgs.answers("sun")]Go To TOP3. images() - image search by duckduckgo.comdefimages(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,)->Generator[Dict[str,Optional[str]],None,None]:"""DuckDuckGo images search. Query params: https://duckduckgo.com/paramsArgs: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, andUse Commercially). Defaults to None.max_results: max number of results. If None, returns results only from the first response. Defaults to None.Yields:dict with image search results."""Examplefromduckduckgo_searchimportDDGSwithDDGS()asddgs:keywords='butterfly'ddgs_images_gen=ddgs.images(keywords,region="wt-wt",safesearch="off",size=None,color="Monochrome",type_image=None,layout=None,license_image=None,max_results=100,)results=[rforrinddgs_images_gen]# asyncwithAsyncDDGS()asaddgs:results=[rasyncforrinaddgs.images('sun',region='wt-wt',safesearch='off',max_results=20)]Go To TOP4. videos() - video search by duckduckgo.comdefvideos(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,)->Generator[Dict[str,Optional[str]],None,None]:"""DuckDuckGo videos search. Query params: https://duckduckgo.com/paramsArgs: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.Yields:dict with videos search results"""Examplefromduckduckgo_searchimportDDGSwithDDGS()asddgs:keywords='tesla'ddgs_videos_gen=ddgs.videos(keywords,region="wt-wt",safesearch="off",timelimit="w",resolution="high",duration="medium",max_results=100,)results=[rforrinddgs_videos_gen]# asyncwithAsyncDDGS()asaddgs:results=[rasyncforrinaddgs.videos('sun',region='wt-wt',safesearch='off',timelimit='y',max_results=10)]Go To TOP5. news() - news search by duckduckgo.comdefnews(keywords:str,region:str="wt-wt",safesearch:str="moderate",timelimit:Optional[str]=None,max_results:Optional[int]=None,)->Generator[Dict[str,Optional[str]],None,None]:"""DuckDuckGo news search. Query params: https://duckduckgo.com/paramsArgs: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.Yields:dict with news search results."""Examplefromduckduckgo_searchimportDDGSwithDDGS()asddgs:keywords='holiday'ddgs_news_gen=ddgs.news(keywords,region="wt-wt",safesearch="off",timelimit="m",max_results=20)results=[rforrinddgs_news_gen]# asyncwithAsyncDDGS()asaddgs:results=[rasyncforrinaddgs.news('sun',region='wt-wt',safesearch='off',timelimit='d',max_results=10)]Go To TOP6. maps() - map search by duckduckgo.comdefmaps(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,)->Iterator[Dict[str,Optional[str]]]:"""DuckDuckGo maps search. Query params: https://duckduckgo.com/paramsArgs:keywords: keywords for queryplace: 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 andlongitude 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.Yields:dict with maps search results"""Examplefromduckduckgo_searchimportDDGSwithDDGS()asddgs:results=[rforrinddgs.maps("school",place="Uganda",max_results=50)]# asyncwithAsyncDDGS()asaddgs:results=[rasyncforrinaddgs.maps('shop',place="Baltimor",max_results=10)]Go To TOP7. translate() - translation by duckduckgo.comdeftranslate(self,keywords:str,from_:Optional[str]=None,to:str="en",)->Generator[Dict[str,Optional[str]],None,None]:"""DuckDuckGo translateArgs:keywords: string or a list of strings to translatefrom_: translate from (defaults automatically). Defaults to None.to: what language to translate. Defaults to "en".Yields:dict with translated keywords."""Examplefromduckduckgo_searchimportDDGSwithDDGS()asddgs:keywords='school'results=[rforrinddgs.translate(keywords,to="de")]# asyncwithAsyncDDGS()asaddgs:results=[rasyncforrinaddgs.translate('sun',to="de")]Go To TOP8. suggestions() - suggestions by duckduckgo.comdefsuggestions(keywords,region:str="wt-wt",)->Generator[Dict[str,Optional[str]],None,None]:"""DuckDuckGo suggestions. Query params: https://duckduckgo.com/paramsArgs:keywords: keywords for query.region: wt-wt, us-en, uk-en, ru-ru, etc. Defaults to "wt-wt".Yields:dict with suggestions results."""Examplefromduckduckgo_searchimportDDGSwithDDGS()asddgs:results=[rforrinddgs.suggestions("fly")]# asyncwithAsyncDDGS()asaddgs:results=[rasyncforrinaddgs.suggestions('sun')]Go To TOP