Fetch DuckDuckGo Web Search Results in 20 lines of Java code

DuckDuckGo API is a fast and light-weight option to fetch web search results. Unfortunately their official API doesn’t seem to work well for all types of search queries. So, the preferred option is to use hit their HTML search and scrape the HTML page for obtaining results.

In the below code, I have followed the same HTML scraping logic to fetch search results from DuckDuckGo. I am using Jsoup library to parse and scrape their search results page.

The only dependency in my pom.xml is as below. You can download the jsoup jar manually and add it to your classpath instead.

<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.3</version>
</dependency>

The main code:

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
private final static String DUCKDUCKGO_SEARCH_URL = "https://duckduckgo.com/html/?q=";
public class DuckDuckGoSearch {

public static void getSearchResults(String query){
Document doc = null;

try {
doc = Jsoup.connect(DUCKDUCKGO_SEARCH_URL + query).get();
Elements results = doc.getElementById("links").getElementsByClass("results_links");

for(Element result: results){

Element title = result.getElementsByClass("links_main").first().getElementsByTag("a").first();
System.out.println("\nURL:" + title.attr("href"));
System.out.println("Title:" + title.text());
System.out.println("Snippet:" + result.getElementsByClass("result__snippet").first().text());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String query = "How to reset my windows password?";
getSearchResults(query);
}
}

IMHO, this might be the fastest way to obtain web search results compared to Google Custom Search or any other current web search APIs .