The Google web services allow you to query Google's web index and to access the Google spell check engine. When performing a search you have access to most features available when searching on Google's web site:
- Retrieve a cached web page from Google
- Enable or disable SafeSearch
- Limit the search results by language
Currently, there are limitations on Google's Web API. Some features that aren't currently available through Google's Web API:
- Search is limited to the Web search. Currently, there isn't any access to search in the Images, Groups, News, Froogle, or Local categories.
- The search modifies are limited to a subset of the search modifiers that are allowed on google.com (See Related Links). For example, the "site:" modifier is allowed from the web api only if additional search terms are included. Google.com allows for "site:" modifier to be used without any additional search terms to list all pages in a site that have been indexed. In addition, some additional search modifies such as "movie:" to display movie times specified by a zip code.
- Searching in Google's Print index is not currently available.
- Google's calculator, currency conversion, word definition, stock quotes, or weather are not currently available through the Google Web API.
To start using the Google Web API, you will need to download the Google Web APIs developer's kit and register for a Google license key on Google's developer page (see Related Links). Once you have downloaded and unzipped the Google developer's kit, include the googleapi.jar file in your Java class path.
You will need to instantiate an instance of the GoogleSearch class and set the key that was provided by Google.
GoogleSearch googleSearch = new GoogleSearch();
//Set the key provided by Google
googleSearch.setKey("your key");
Next, we need to set the query string that you want to search for in the Google index. If you want to use any search modifiers, include them in the query string. To limit our search results, we set the maximum number of matches to 10.
//Search query string
googleSearch.setQueryString("population of Japan");
//Limit the search results to 10
googleSearch.setMaxResults(10);
For our final steps, we perform the search. After the search is complete, we get the search result elements. The search result elements contain the page title, url, and page snippet.
GoogleSearchResult searchResult = googleSearch.doSearch();
GoogleSearchResultElement[] results = searchResult.getResultElements();
On a final note, the search result elements contain HTML formatting to show the hits within the results. In our example, we use a Java's regular expression support to remove the HTML tags. This is a quick and easy way to remove the HTML tags from text.
String title = results[index].getTitle();
title = title.replaceAll("<.*?>", "");
The complete Google Web API example is below.
package tutorial;
import com.google.soap.search.*;
public class Google
{
public Google()
{
try
{
GoogleSearch googleSearch = new GoogleSearch();
//Set the clientKey to key provided by Google
googleSearch.setKey("your key");
//Search query string
googleSearch.setQueryString("population of Japan");
//Limit the search results to 10
googleSearch.setMaxResults(10);
GoogleSearchResult searchResult = googleSearch.doSearch();
GoogleSearchResultElement[] results = searchResult.getResultElements();
if (results.length == 0)
{
System.out.println("No match found.");
}
else
{
for (int index = 0; index < results.length; index++)
{
//Title
String title = results[index].getTitle();
title = title.replaceAll("<.*?>", "");
System.out.println(title);
//URL
String url = results[index].getURL();
System.out.println(url);
//Title
String summary = results[index].getSnippet();
summary = summary.replaceAll("<.*?>", "");
System.out.println(summary);
System.out.println();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Google googleDemo = new Google();
}
}
discuss this topic to forum
