38

What I want to do is just submitting string queries (the equivalent of typing into the search bar by hand) and saving the URL of the first results (if there are any).

I have asked a similar question ( Python search scraping) and the answer to it works well with google my problem is that I can't find Duckduckgo's search api address anywhere. Could you help me or suggest antoher way of doing this?

Community
  • 1
  • 1
Nesa
  • 2,895
  • 2
  • 12
  • 19

3 Answers3

76

tl;dr -- Screen scrape https://duckduckgo.com/html/?q={search_terms}

As other answerers have mentioned, the URL for DuckDuckGo's limited search API is

http://api.duckduckgo.com/?q=x&format=json

x being the search terms you're looking for.

However, please note that this is not a full search API. As DuckDuckGo's API page mentions,

This API does not include all of our links, however. That is, it is not a full search results API or a way to get DuckDuckGo results into your applications beyond our instant answers. Because of the way we generate our search results, we unfortunately do not have the rights to fully syndicate our results. For the same reason, we cannot allow framing our results without our branding. Please see our partnerships page for more info on guidelines and getting in touch with us.

This is an Instant Answer API, and not a full results API. However, there are some Web links within it, e.g. official sites.

So for your stated purpose of

just submitting string queries (the equivalent of typing into the search bar by hand) and saving the URL of the first results (if there are any).

api.duckduckgo.com will not get you what you want.

Your best bet is probably to just screen scrape the non-JS, web version of DuckDuckGo:

https://duckduckgo.com/html/?q=x

looking for elements with a selector of something like div.result or div.web-result.

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
ethanbustad
  • 1,594
  • 1
  • 16
  • 27
  • 1
    Thanks, so the last three lines are expounded in the linked question's answers? – Nesa Aug 16 '16 at 17:24
  • @Nesa yep! Especially if you're interested in doing the screen-scraping in python. – ethanbustad Aug 17 '16 at 19:42
  • Thanks for the experienced tip. Care to explain the benefits of using the JSON param? – ntk4 Jan 14 '18 at 18:32
  • The JSON param just tells the API to return you results in JSON format. I believe the only other option is `format=xml`. It may give you a JSON response without explicitly asking for it, but if so it's an undocumented feature and could change without notice. – ethanbustad Jun 18 '18 at 17:00
  • I have something implemented for Image search here, This would be very similar to implement - https://github.com/deepanprabhu/duckduckgo-images-api – Deepan Prabhu Babu Jan 16 '19 at 01:55
  • is there anyway to add time and location to query? – chovy Feb 07 '21 at 08:07
  • The api.duckduckgo.com domain doesn't seem to work for me anymore. Though just using the base `duckduckgo.com` domain with the `format=json` param does seem to work for me, ie. `http://duckduckgo.com/?q=x&format=json` for query `x`. – Cory Gross Oct 05 '22 at 11:40
3

just change with whatever you want to search for. This will output the results in JSON

https://api.duckduckgo.com/?q=<your search string>&format=json&pretty=1&no_html=1&skip_disambig=1

I think this is what you are asking for

Steve
  • 1,371
  • 9
  • 13
2

www.api.duckduckgo.com/?q=Search&format=json&pretty=1

Example: http://api.duckduckgo.com/?q=DuckDuckGo&format=json&pretty=1

The response is a JSON Object, you can find the address of the first link:

response.Results[0].FirstURL

You can find that out by putting the response into https://jsonformatter.curiousconcept.com/

Hope it helped :)

Simon Meusel
  • 200
  • 3
  • 16