com.google.soap.search
.
Here are the brief description of each class.
GoogleSearch
: you access Google API through this class. Use setKey
to set the key.
Use setQueryString
to set the query. Use doSearch
to start the search. There are other properties that you can set. GoogleSearchResult
: the result returns by a GoogleSearch. GoogleSearchResultElement
: individual element of the GoogleSearchResult.
GoogleSearchDirectoryCategory
: represents directory category portion of GoogleSearchResult. GoogleSearchFault
: an exception when something wrong happens. 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 :
type in your licencse key here
googleapi.jar
C:\java\googleapi\googleapi.jar
.
javac -classpath .;C:\java\googleapi\googleapi.jar GoogleTest.java
java -classpath .;C:\java\googleapi\googleapi.jar GoogleTest
GoogleSearchFault
GoogleSearch search = new GoogleSearch()
: create an instance of GoogleSearch
public static String key = ....
: your key. search.setKey(key)
: you have to set the key before you do any searchsearch.setQueryString(String searchTerm)
: your search query. In my example, I search for
results contain Charles
and Li
. If you want to search result contains
Charles Li
(2 words together). Use the following line :
String searchTerm = "\"Charles Li\"";It is same as typing
"Charles Li"
in google.
GoogleSearchResult result = search.doSearch()
: execute the search. link:http://www.cnn.com
in the search query.
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. |
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. |
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 |
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. |
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=1021ComparePopularity.java
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