How to retrieve data from google programmitcally?

Here is the post for retrieving the data from google.

For it, i am using java language in eclipse editor.

1. Firstly make a project in the eclipse.

2. Add the Jsoup library inside your project and configure the path.
Download jsoup from
  Learn, how to add library in the project and configure?

3. Create a java class with name Mainn inside your project and paste the code.

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.Jsoup;

public class Mainn {
private static Pattern patternDomainName;
 private Matcher matcher;
 private static final String DOMAIN_NAME_PATTERN
= “([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}”;
 static {
patternDomainName = Pattern.compile(DOMAIN_NAME_PATTERN);
 public String getDomainName(String url){

String domainName = “”;
matcher = patternDomainName.matcher(url);
if (matcher.find()) {
domainName =;
return domainName;


 private Set<String> getDataFromGoogle(String query) {

Set<String> result = new HashSet<String>();
String request = “” + query + “&num=20”;
System.out.println(“Sending request…” + request);

try {

// need http protocol, set this as a Google bot agent 🙂
Document doc = Jsoup
 “Mozilla/5.0 (compatible; Googlebot/2.1; +”)

// get all links
Elements links =“a[href]”);
for (Element link : links) {

String temp = link.attr(“href”);
                               //use regex to get domain name


} catch (IOException e) {

return result;
 public static void main(String[] args) {
// TODO Auto-generated method stub
Mainn obj = new Mainn();

// change the query according to your need.
Set<String> result = obj.getDataFromGoogle(“sunny“);
for(String tempp : result){

My output is:

Leave a Comment