Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 51 Looping Around the 10-Result Limit

figs/moderate.giffigs/hack51.gif

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.

51.1 The Code

#!/usr/local/bin/perl
# 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 results

use 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 <= ($loops-1)*10; $offset += 10) {
 # Query Google
 my $results = $google_search -> 
  doGoogleSearch(
    $google_key, $query, $offset, 10, "false", "", "false",
    "", "latin1", "latin1"
  );
 
 # No sense continuing unless there are more results
 last unless @{$results->{resultElements}};
 
 # Loop through the results
 foreach my $result (@{$results->{'resultElements'}}) {
 
  # Print out the main bits of each result
  print
   join "\n",  
   ++$number,
   $result->{title} || "no title",
   $result->{URL},
   $result->{snippet} || 'no snippet',
   "\n";
 
 }
}

Notice that the script tells Google which set of 10 results it's after by passing an offset ($offset). The offset is increased by 10 each time ($offset += 10).

51.2 Running the Script

Run this script from the command line, passing it your preferred query:

$ perl looply.pl "query"

51.3 The Results

% perl looply.pl
Usage: perl looply.pl <query>
% perl looply.pl "learning perl"
1
oreilly.com -- Online Catalog: Learning Perl, 3rd Edition
http://www.oreilly.com/catalog/lperl3/
... Learning Perl, 3rd Edition Making Easy Things 
Easy and Hard Things Possible By Randal<br> L. Schwartz, Tom Phoenix 
3rd Edition July 2001 0-596-00132-0, Order Number ... 
 
...
29
Intro to Perl for CGI
http://hotwired.lycos.com/webmonkey/98/47/index2a.html
... Some people feel that the benefits of learning 
Perl scripting are few.<br> But ... part. That's right. 
Learning Perl is just like being a cop. ...  
30
WebDeveloper.com ®: Where Web Developers and Designers Learn How ... 
http://www.webdeveloper.com/reviews/book6.html
... Registration CreditCard Processing Compare Prices. 
Learning Perl. Learning<br> Perl, 2nd Edition. 
Publisher: O'Reilly Author: Randal Schwartz ...    

51.4 See Also

    Previous Section Next Section


         Main Menu
    Main Page
    Table of content
    Copyright
    Dedication
    Credits
    Foreword
    Preface
    Chapter 1. Searching Google
    Chapter 2. Google Special Services and Collections
    Chapter 3. Third-Party Google Services
    Chapter 4. Non-API Google Applications
    Chapter 5. Introducing the Google Web API
    5.1 Hacks #50-59
    5.2 Why an API?
    5.3 Signing Up and Google's Terms
    5.4 The Google Web APIs Developer's Kit
    5.5 Using the Key in a Hack
    5.6 What's WSDL?
    5.7 Understanding the Google API Query
    5.8 Understanding the Google API Response
    Hack 50 Programming the Google Web API with Perl
    Hack 51 Looping Around the 10-Result Limit
    Hack 52 The SOAP::Lite Perl Module
    Hack 53 Plain Old XML, a SOAP::Lite Alternative
    Hack 54 NoXML, Another SOAP::Lite Alternative
    Hack 55 Programming the Google Web API with PHP
    Hack 56 Programming the Google Web API with Java
    Hack 57 Programming the Google Web API with Python
    Hack 58 Programming the Google Web API with C# and .NET
    Hack 59 Programming the Google Web API with VB.NET
    Chapter 6. Google Web API Applications
    Chapter 7. Google Pranks and Games
    Chapter 8. The Webmaster Side of Google
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele