Sometimes, I wondered if I could search Google without using a browser. Then I found that Google has a web service which we can use for this purpose. Here is the sample client for Google Search Web Service.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import org.json.JSONObject; public class SampleSearch { public static void main(String[] args) throws Exception { //q=javacodingtutorial.blogspot.com - This is the string you want to search //userip=USERS-IP-ADDRESS - This gives the IP address of the user String temp = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&" + "q=javacodingtutorial.blogspot.com&userip=USERS-IP-ADDRESS"; URL url = new URL(temp); URLConnection connection = url.openConnection(); String line; StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream())); System.out.println(connection.getURL()); System.out.println(); while ((line = reader.readLine()) != null) { builder.append(line); } JSONObject json = new JSONObject(builder.toString()); System.out.println(json); } }
This code will return Google Search Results in JSON objects.
Here is the sample output Download
But, there is a problem with this code if you want for than 4 results. Because this Webservice
returns only 4 results at a time. But, there is a solution to this problem as well. we have another one parameter which we can use. we can change the url to -
https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=javacodingtutorial.blogspot.com&userip=USERS-IP-ADDRESS&start=0 this will return results with indexes 0,1,2,3.
Similarly if we use the URL - https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=javacodingtutorial.blogspot.com&userip=USERS-IP-ADDRESS&start=4 this will return results with indexes 4,5,6,7.
So, to return 100 results (you can want any other number), we will modify our code like this:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import org.json.JSONObject; public class SampleSearch { public static void main(String[] args) throws Exception { String temp = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&" + "q=javacodingtutorial.blogspot.com&userip=USERS-IP-ADDRESS"; for (int i = 0; i < 100; i = i + 4) { System.out.println("Time : "+i+"\n"); URL url = new URL(temp + "&start="+i); URLConnection connection = url.openConnection(); connection.addRequestProperty("Referer", "www.facebook.com"); String line; StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream())); System.out.println(connection.getURL()); System.out.println(); while ((line = reader.readLine()) != null) { builder.append(line); } JSONObject json = new JSONObject(builder.toString()); System.out.println(json); } } }
Using this code, we can search for as many result as we want using Google & Java together.
"The aim of education is the knowledge not of facts but of values." - William Ralph Inge
No comments :
Post a Comment