Tuesday, 20 June 2017

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 http://jsoup.org/download.
  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.io.IOException;
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.select.Elements;
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 = matcher.group(0).toLowerCase().trim();
return domainName;


 private Set<String> getDataFromGoogle(String query) {

Set<String> result = new HashSet<String>();
String request = "https://www.google.com/search?q=" + 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; +http://www.google.com/bot.html)")

// get all links
Elements links = doc.select("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:

No comments:

Post a comment

All about journals and research paper | What is impact factor? | how the impact factor is calculated? | who calculate the impact factor? Scopous journals

Figure 1: One of my Research Papers When the Scholars are in their Master or PhD or in any research field. They are supposed to writ...