Presents your JAVA E-NEWSLETTER for November 21, 2002 <-------------------------------------------> SEARCH WITH GOOGLE WITH A FREE API HTML scraping--using a program to read data from a Web page--is a dodgy affair, because you have to deal with poor HTML code, with changing pages, and with companies who are trying to stop your scraping from working. However, Google's release of a Java client API saves us from the painful scraping of that search engine. Google is currently offering a free beta service that provides a client-side API that communicates with their server. The service allows developers to issue search requests, access the Google cache, and check on the spelling of words. The service itself is a Simple Object Access Protocol (SOAP) service; this is an XML protocol for making requests in an object-oriented way. Most importantly, this means that the Google service is open to all languages that can support SOAP. Google also supplies a Java library to relieve developers from the SOAP setup process. To get access to the API, you download the code and sign up for a client key. The key, made up of a string of 32 alphanumeric characters, allows a developer to make 1,000 requests a day. http://www.google.com/apis/ The Java API begins with a GoogleSearch class. The client key is associated with this class and one of three searches may be requested. First, a keyword search may be performed with the following code: import com.google.soap.search.*; .... GoogleSearch searcher = new GoogleSearch( ); // insert your own key here searcher.setKey("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); searcher.setQueryString("java builder"); GoogleSearchResult result = searcher.doSearch( ); System.out.println(result); Second, a cached page can be accessed: import com.google.soap.search.*; .... GoogleSearch searcher = new GoogleSearch( ); // insert your own key here searcher.setKey("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); byte[ ] page = searcher.doGetCachedPage("http://java.sun.com"); System.out.println(new String(page)); Lastly, a spelling suggestion can be obtained: import com.google.soap.search.*; .... GoogleSearch searcher = new GoogleSearch( ); // insert your own key here searcher.setKey("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); String suggestion = searcher.doSpellingSuggestion("jave"); System.out.println(suggestion); The Google Web API is a neat tool that developers can tuck into their toolset or integrate into internal applications. The limitation on a thousand requests a day stops a small industry starting up at Google's expense, and provides a good example of a Web service. ----------------------------------------