Loop 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, 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 10. The next, 11 through 20. And so forth.

This hack builds on the basic Perl script googly.pl [Hack #92] that we showed you in the previous hack. To get at more than the top 10 results, no matter the programming language you’re using, you’ll have to create a loop.

Warning

Bear in mind that each and every query counts against your daily allotment. Loop three times and you’ve used up three queries. Ten, and you’re down 10. While this doesn’t seem like much given your quota of 1,000 queries a day, you’d be surprised how quickly you can reach the bottom of the cookie jar without knowing where they all went.

The Code

Save the following code to a text file named looply.pl. Again, remember to replace insert key here with your Google API key, as explained in “Using Your Google API Key” earlier in this chapter.

Tip

In addition to the Google API Developer’s Kit, you’ll need the SOAP::Lite Perl module [Hack #93] installed before running this hack.

The alterations to the previous hack needed to support looping through more than the first 10 results are called out in bold.

#!/usr/local/bin/perl
# looply.pl # A typical Google Web API ...

Get Google Hacks, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.