Using Google Search API from Java
Recently I’ve discovered ability to search in Google from Java program in a way different from brute “crawling”.
And I’ve found out two options:
With this API you can issue search requests to Google’s index of billions of web pages and receive results as structured data, access information in the Google cache, and check the spelling of words. Google SOAP Search API is implemented as a web service and all you need is just get WSDL and call remote methods through SOAP protocol. Usage of this service is limited to 1000 requests per day.
But as you can read on SOAP API site
As of December 5, 2006, we are no longer issuing new API keys for the SOAP Search API. Developers with existing SOAP Search API keys will not be affected.
So you aren’t able to get API key (I found the old one in Internet).
And Google encourages you to use the AJAX Search API, which is described below.
Example of code that perform search request and show obtained results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import com.google.soap.search.*; import java.io.*; public class SimpleGoogleDemo { public static void main(String[] args) { // Create a Google Search object, set our authorization key GoogleSearch s = new GoogleSearch(); String clientKey="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; s.setKey(clientKey); s.setQueryString("vera.org.ua"); GoogleSearchResult result = null; // Depending on user input, do search or cache query, then print out result try { result = s.doSearch(); } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } // if we made it here, the search went through System.out.println("Google Search Results:"); System.out.println("======================"); if (result!= null) System.out.println(result.toString()); } } |
To run this code you need to have googleapi.jar in your classpath. This jar file contains very useful and easy-to-use class com.google.soap.search.GoogleSearch, which does all search work.
Usage of this unlimited and you can make as many request as you want.
Despite the fact that Google in this API FAQ say “The Google AJAX Search API is currently available only for websites.” ability to use it exists
Example of code that perform search request and show obtained results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.io.*; import java.net.*; public class GoogleAJAXSearchAPI { private static String endpointURL = "http://www.google.com/uds/GwebSearch?"+ "callback=GwebSearch.Raw" + "Completion&context=0&lstkp=0&rsz=small&hl=en&" + "sig=8656f49c146c5220e273d16b4b6978b2&q=vera.org.ua&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxx&v=1.0"; public static void main(String[] args) throws Exception { URLConnection uc = new URL(endpointURL).openConnection(); HttpURLConnection connection = (HttpURLConnection) uc; connection.setDoOutput(true); connection.setRequestMethod("GET"); connection.connect(); String line; InputStream inputStream = null; try { inputStream = connection.getInputStream(); } catch (IOException e) { inputStream = connection.getErrorStream(); } BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream)); while ((line = rd.readLine()) != null) { System.out.println(line); } } } |
To run this code you just need to get API key here.
So, have fun ![]()
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
April 25th, 2008 at 5:15 pm
I’ve recently looked for API to some giant search engine.
And stopped on Yahoo’s API. Reason is simple - they’re provide SOAP and REST access to their engine. More handy approach, eh?
April 25th, 2008 at 5:58 pm
But Google is Google
August 12th, 2008 at 9:13 am
helloo.. could anybody help me..
here in the above general code of google soap protocol.. i need google api jar file
in the above link i am not able to download, there was an internal error with webserver..
so could anybody forward to my mail account pleasee..
harsha.siri@gmail.com
thanking you forever…. harsha
August 12th, 2008 at 11:19 am
>>in the above link i am not able to download, there was an internal error with webserver..
Sorry for inconvenience. This problem was fixed.