Google API Notes

Charles Li
Copyright © 2005-2006
secret
Charles' Java Articles | charlesli.org

Google

Google

Google API

The main website is http://www.google.com/apis/. Steps to obtain it (http://www.google.com/apis/).
  1. download the program and upzip the file. You may want to place the files in another folder. Remember the path of the folder.
  2. create an account and get a license key.
Limitation: you can only made 1000 queries each day!

Classes overview

There are 5 main classes. All the classes are in package com.google.soap.search. Here are the brief description of each class. The usage of all there classes are pretty easy. The classes have lot of get/set methods that you can get/set properties of the class. The only thing you need to do is to browse the API and see what kind of things you can get/set. In below is a very simple program to search my name "Charles Li" and find out how many pages contains my name.

GoogleTest.java

import com.google.soap.search.*;

public class GoogleTest {
    public static String key = "      type in your licencse key here         ";
    public static void main(String[] args) {	
        String searchTerm = "Charles Li";
	try {
	      GoogleSearch search = new GoogleSearch();
	      search.setKey(key);
	      search.setQueryString(searchTerm);
	      GoogleSearchResult result = search.doSearch();	
	      System.out.println(result.getEstimatedTotalResultsCount());
	} catch (GoogleSearchFault gsf) {
	      System.out.println("Google Search Fault: " + gsf.getMessage());
	}
    }
}
Steps to compile and run my program :
  1. key: In the program, substitute your key in       type in your licencse key here      
  2. googleapi.jar: First of all, make sure that you know where your googleapi.jar file locates. You can do a search for googleapi.jar
  3. . Example the full path of the jar file on my computer is C:\java\googleapi\googleapi.jar.
  4. compile: compile the program by
    javac -classpath .;C:\java\googleapi\googleapi.jar GoogleTest.java
    
  5. execute: Run the program by
    java -classpath .;C:\java\googleapi\googleapi.jar GoogleTest
    
Explanation:

More about queryString

You can get more help on Example, if you like to see how many webpages link to "http://www.cnn.com". You can type in link:http://www.cnn.com in the search query.

Useful methods for GoogleSearch class

 byte[] doGetCachedPage(java.lang.String url)
          Retrieve a cached web page from Google.
 GoogleSearchResult doSearch()
          Invoke the Google search.
 java.lang.String doSpellingSuggestion(java.lang.String phrase)
          Ask Google to return a spelling suggestion for a word or phrase.
 void setFilter(boolean on)
          Enable or disable the "related-queries" filter.
 void setKey(java.lang.String key)
          Set the user key used for authorization by Google SOAP server.
 void setLanguageRestricts(java.lang.String lr)
          Set the language restricts for the search.
 void setMaxResults(int maxResults)
          Set the maximum number of results to be returned.
 void setQueryString(java.lang.String q)
          Set the query string for this search.
 void setRestrict(java.lang.String restrict)
          Set the restrict.
 void setSafeSearch(boolean safeSearch)
          Enable or disable SafeSearch.
 void setStartResult(int start)
          Set the index of the first result to be returned.

Useful methods in GoogleSearchResult class

 GoogleSearchDirectoryCategory[] getDirectoryCategories()
          Returns an array of directory categories for this result.
 boolean getDocumentFiltering()
          Returns true if and only if filtering was performed on the search results, which happens only if: (a) you requested filtering, and (b) filtering actually occurred.
 int getEndIndex()
          Returns the index (1-based) of the last search result in the result elements.
 int getEstimatedTotalResultsCount()
          Returns the estimated total number of results returned for the query.
 boolean getEstimateIsExact()
          Returns true if and only if the estimated number of results is exact.
 GoogleSearchResultElement[] getResultElements()
          Returns an array of result elements.
 java.lang.String getSearchComments()
          Returns string intended for display to an end user.
 java.lang.String getSearchQuery()
          Returns the query string that generated this result.
 double getSearchTime()
          Returns total server time to process the query (in seconds).
 java.lang.String getSearchTips()
          Returns a string intended for display to the end user; provides instructive suggestions on how to use Google.
 int getStartIndex()
          Returns the index (1-based) of the first search result in the result elements.

Useful methods in GoogleSearchResultElement class

 java.lang.String getCachedSize()
          Returns the size in kilobytes of the cached version.
 GoogleSearchDirectoryCategory getDirectoryCategory()
          Returns the directory category of this result element
 java.lang.String getDirectoryTitle()
          Returns the title that appears in the directory if this document is contained in the ODP directory.
 java.lang.String getHostName()
          Returns the host name in the case where multiple results come from a single host.
 boolean getRelatedInformationPresent()
          Returns true if and only if the "related:" special query term is supported for this URL.
 java.lang.String getSnippet()
          Returns a snippet which shows the query in context on the URL where it appears.
 java.lang.String getSummary()
          Returns the ODP summary if this document is contained in the ODP directory.
 java.lang.String getTitle()
          Returns the title of the search result, formatted as HTML.
 java.lang.String getURL()
          Returns the absolute URL of the search result

Useful methods in GoogleSearchDirectoryCategory class

java.lang.String getFullViewableName()
          Returns the ODP directory name for the current ODP category
 java.lang.String getSpecialEncoding()
          Returns the encoding scheme of the directory information.

More examples

GoogleTest2.java
import com.google.soap.search.*;

public class GoogleTest2 {
    public static String key ="           type in your licencse key here          ";
    public static void main(String[] args) {	
        String searchTerm = "Matrix Movie";
	try {
	      GoogleSearch search = new GoogleSearch();
	      search.setKey(key);
	      search.setQueryString(searchTerm);
	      GoogleSearchResult result = search.doSearch();	
	      
	      
	      int startIndex = result.getStartIndex() - 1;
	      int endIndex = result.getEndIndex()-1;
	      
	      GoogleSearchResultElement[] resultElement = result.getResultElements();
	      for(int i = startIndex; i < =endIndex; i++) {
		  System.out.println(resultElement[i].getURL());
	      }
	      
	      
	} catch (GoogleSearchFault gsf) {
	    System.out.println("Google Search Fault: " + gsf.getMessage());
	}
    }
}
Output(on 2003/5/29)
http://www.whatisthematrix.com/
http://www.movie-page.com/1999/Matrix.htm
http://www.joblo.com/moviescreensavers/matrixsavers.htm
http://www.joblo.com/moviescreensavers/matrixscreensaver.htm
http://awesomehouse.com/matrix/intro.html
http://www.3dgamers.com/games/entermatrix/
http://www.anzwers.org/free/thematrix/
http://www.mediacircus.net/matrix.html
http://www.all-time-movie-posters.com/the-matrix.php
http://www.computergames.ro/download.php?optiune=show_download&did=1021
ComparePopularity.java
  • Write a program to compare the popularity of different things (example, different software) by comparing the number of search result.
  • import com.google.soap.search.*;
    
    public class ComparePopularity {
        public static String key = "           type in your licencse key here          ";
        public static void main(String[] args) throws Exception {	
            String[] names = {"Bill Clinton", "Al Gore", "George Bush", 
    	                   "Jennifer Lopez", "George Lucas", "Michael Jackson"};
    	GoogleSearch search = new GoogleSearch();
    	search.setKey(key);
    
    	for(int i = 0; i < names.length; i++) {
    	    search.setQueryString("\"" + names[i]+ "\"");
    	    GoogleSearchResult result = search.doSearch();
    	    System.out.println(names[i] + "\t" + result.getEstimatedTotalResultsCount() );
    	}
        }
    }
    
    Output(2003/5/29)
    Bill Clinton    1040000
    Al Gore   	859000
    George Bush     1090000
    Jennifer Lopez  1190000
    George Lucas    413000
    Michael Jackson 860000
    

    Charles' Java Articles. | charlesli.org |
    First posted: 5/29/2003
    Last modifed: 9/15/2005
    Copyright © 2003-2005 Charles Li