Java Reference
In-Depth Information
The
getResults
function
returns
a
Collection
of
GoogleSearchResultElement objects. These objects are displayed.
for( GoogleSearchResultElement element:c)
{
Each element is displayed. For each element, the getLinkCount function is called
to display the number of links found.
StringBuilder str = new StringBuilder();
str.append(getLinkCount(element.getURL()));
str.append(":");
str.append(element.getTitle() );
str.append("(");
str.append(element.getURL());
str.append(")");
System.out.println(str.toString());
}
Each of the elements will be displayed.
Searching Google
In the last section, we saw that the getResults function returns a list of pages from
the Google search engine. In this section, we will see how the getResults function
works. It begins by creating a collection of GoogleSearchResultElement ob-
jects.
Collection<GoogleSearchResultElement> result =
new ArrayList<GoogleSearchResultElement>();
int resultCount = 0;
int top = 0;
Next, the getResults function begins a loop and obtains the search results. Google
returns the search results ten items at a time.
do
{
search.setQueryString(query);
search.setStartResult(top);
GoogleSearchResult r = null;
The search is performed.
try
{
r = search.doSearch();
} catch (GoogleSearchFault e)
{
e.printStackTrace();
Search WWH ::




Custom Search