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.

Steps:-
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
.connect(request)
.userAgent(
 "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
.timeout(5000).get();

// get all links
Elements links = doc.select("a[href]");
for (Element link : links) {

String temp = link.attr("href");
if(temp.startsWith("/url?q=")){
                               //use regex to get domain name
result.add(getDomainName(temp));
}

}

} catch (IOException e) {
e.printStackTrace();
}

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){
//System.out.println(result.size());
      System.out.println(tempp);
       
}
}
}



My output is:


Comments

Popular posts from this blog

Accounting Multiple Choice Questions with answers | Download PDF for MCQs

Difference between Data Mining and Knowledge Discovery (KDD)