Google::Search - Interface to the Google AJAX Search API and suggestion API (DEPRECATED)
version 0.028
NOTE: The Google AJAX Search API has been deprecated: http://developers.google.com/web-search/docs/
my $search = Google::Search->Web( query => "rock" ); while ( my $result = $search->next ) { print $result->rank, " ", $result->uri, "\n"; }
You can also use the single-argument-style invocation:
Google::Search->Web( "query" )
The following kinds of searches are supported
Google::Search->Local( ... ) Google::Search->Video( ... ) Google::Search->Blog( ... ) Google::Search->News( ... ) Google::Search->Image( ... ) Google::Search->Patent( ... )
You can also take advantage of each service's specialized interface
# The search below specifies the latitude and longitude: $search = Google::Search->Local( query => { q => "rock", sll => "33.823230,-116.512110" }, ... ); my $result = $search->first; print $result->streetAddress, "\n";
You can supply an API key and referrer (referer) if you have them
my $key = ... # This should be a valid API key, gotten from: # http://code.google.com/apis/ajaxsearch/signup.html my $referrer = "http://example.com/" # This should be a valid referer for the above key $search = Google::Search->Web( key => $key, referrer => $referrer, # "referer =>" Would work too query => { q => "rock", sll => "33.823230,-116.512110" } );
Get suggestions from the unofficial Google suggestion API using suggest
suggest
my $suggestions = Google::Search->suggest( $term )
Google::Search is an interface to the Google AJAX Search API (http://code.google.com/apis/ajaxsearch/).
Currently, their API looks like it will fetch you the top 64 results for your search query.
You may want to sign up for an API key, but it is not required. You can do so here: http://code.google.com/apis/ajaxsearch/signup.html
Prepare a new search object (handle)
You can configure the search by passing the following to new:
new
query The search phrase to submit to Google Optionally, this can also be a hash of parameters to submit. You can use the hash form to take advantage of each service's varying interface. Make sure to at least include a "q" parameter with your search service The service to search under. This can be any of: web, local, video, blog, news, book, image, patent start Optional. Start searching from "start" rank instead of 0. Google::Search will skip fetching unnecessary results key Optional. Your Google AJAX Search API key (see Description) referrer Optional. A referrer that is valid for the above key For legacy purposes, "referer" is an acceptable spelling
Both query and service are required
query
service
Returns a Google::Search::Result representing the first result in the search, if any.
Returns undef if nothing was found
An iterator for $search. Will the return the next result each time it is called, and undef when there are no more results.
Returns a Google::Search::Result
Returns a Google::Search::Result corresponding to the result at <rank>
These are equivalent:
$search->result( 0 ) $search->first
Returns Google::Search::Result list which includes every result Google has returned for the query
In scalar context an array reference is returned, a list otherwise
An empty list is returned if nothing was found
Returns a Google::Search::Result list
This method will iterate through each result in the search, passing the result to <code> as the first argument. If <code> returns true, then the result will be included in the returned list
In scalar context this method returns the number of matches
Returns a Google::Search::Result that is the first to match <code>
This method will iterate through each result in the search, passing the result to <code> as the first argument. If <code> returns true, then the result will be returned and iteration will stop.
Returns a Google::Search::Error if there was an error with the last search
If you receive undef from a result access then you can use this routine to see if there was a problem
warn $search->error->reason; warn $search->error->http_response->as_string; # Etc, etc.
This will return undef if no error was encountered
Return a nested array from the Google auto-complete suggestion service. Each inner array consists of: the suggestion, the number of results, and the rank of the suggestion:
my $suggestions = Google::Search->suggest( 'monkey' ) print $suggestions->[0][0] # "monkey bread recipe" print $suggestions->[0][1] # "413,000 results" print $suggestions->[0][2] # 0 for my $suggestion ( @$suggestions ) { ... }
To override the language (or any query parameter or to add in your own parameters), pass in an array:
# Get the results back in German (de) Google::Search->suggest( [ hl => 'de' ], 'monkey' )
To alter the URI hostname/path or to give a custom user agent, pass in a hash:
Google::Search->suggest( [ hl => 'de' ], 'monkey', { host => 'clients1.google.de', agent => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)' } )
The passing order of the array, hash, and string does not matter
Robert Krimen <robertkrimen@gmail.com>
This software is copyright (c) 2012 by Robert Krimen.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Google::Search, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Google::Search
CPAN shell
perl -MCPAN -e shell install Google::Search
For more information on module installation, please visit the detailed CPAN module installation guide.