Looping Around the 10-Result Limit
If you want more than 10 results, you’ll have to loop.
The Google API returns only 10 results per query. Ten results is plenty for some queries, but for most applications, 10 results barely scratches the surface. If you want more than 10 results, you’re going to have to loop, querying for the next set of 10 each time. The first query returns the top ten. The next, 11 through 20. And so forth.
This hack builds on the basic query shown in [Hack #50]. To get at more than the top 10 results, no matter the programming language you’re using, you’ll have to create a loop. The example is in Perl, because that’s what most of the hacks in this book are written in. Alterations to support looping are shown in bold.
# looply.pl# A typical Google Web API Perl script
# Usage: perl looply.pl <query># Your Google API developer's key my $google_key='insert key here'; # Location of the GoogleSearch WSDL file my $google_wdsl = "./GoogleSearch.wsdl";
# Number of times to loop, retrieving 10 results at a time
my $loops = 3; # 3 loops x 10 results per loop = top 30 resultsuse strict; # Use the SOAP::Lite Perl module use SOAP::Lite; # Take the query from the command-line my $query = shift @ARGV or die "Usage: perl looply.pl <query>\n"; # Create a new SOAP::Lite instance, feeding it GoogleSearch.wsdl my $google_search = SOAP::Lite->service("file:$google_wdsl");
# Keep track of result number
my $number = 0;
for (my $offset = 0; $offset ...