Example - How to search Google using Java

google_search_img
Following is the sample code for searching the Google and getting some top results.

This example uses jsoup for sending request and parsing the html response.

Dependency

Jsoup dependency can be downloaded from here : http://jsoup.org/download

Example - search Google with the query "java tutorials"

package com.codeforeach.demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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;

public class GoogleSearchDemo {

  // pattern for extracting the link such as www.codeforeach.com/java/ ( domain
  // name + path )
  private static final Pattern p = Pattern
      .compile("([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/[^&]*)*");

  public static void main(String[] args) throws IOException {
    String searchQuery = "java tutorials";
    List<String> links = searchGoogle(searchQuery);
    for (String link : links) {
      System.out.println(link);
    }

  }

  public static List<String> searchGoogle(String searchQuery) throws IOException {
    List<String> result = new ArrayList<String>();
    // lets get the top results counting to nearly 15
    String request = "https://www.google.com/search?q=" + searchQuery + "&num=15";

    Document doc = Jsoup.connect(request)
        .userAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)").get();
    // get the required content from response . Here ( h3 a ) is the selector
    // pattern for selecting all heading links
    Elements links = doc.select("h3 a[href]");

    for (Element link : links) {
      String hrefValue = link.attr("href");
      if (hrefValue.startsWith("/url?q="))
        result.add(extractLink(hrefValue));
    }

    return result;
  }

  // extract required link from href value
  private static String extractLink(String href) {
    String result = null;
    Matcher m = p.matcher(href);

    if (m.find()) {
      result = m.group();
    }

    return result;
  }

}
www.tutorialspoint.com/java/
www.javatpoint.com/java-tutorial
docs.oracle.com/javase/tutorial/
javabeginnerstutorial.com/core-java/
www.udemy.com/java-tutorial/
tutorials.jenkov.com/java/
www.youtube.com/watch%3Fv%3Dr59xYe3Vyks
www.youtube.com/watch%3Fv%3DCfd9DOnuF9w
beginnersbook.com/java-tutorial-for-beginners-with-examples/
www.freejavaguide.com/corejava.htm
www.java2s.com/Tutorial/Java/CatalogJava.htm
www.vogella.com/tutorials/java.html
www.lynda.com/Java-training-tutorials/1077-0.html
courses.coreservlets.com/Course-Materials/java.html
www.learnjavaonline.org/

 

 

1 Comment

  1. I blog frequently and I genuinely thank you for your content.
    This great article has truly peaked my interest. I will bookmark your website and keep checking for new information about once per week.
    I opted in for your RSS feed as well.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

www.000webhost.com