Previous Section  < Day Day Up >  Next Section

Hack 26. Feel Really Lucky

Take the domain in which the first result of a query appears and do more searching within that domain.

Does Google make you feel lucky? How lucky? Sometimes as lucky as the top result is, more results from the same domain are just as much so.

This hack performs two Google queries. It first saves the domain of the top result of the first search is saved. Then it runs the second query, searching only the saved domain for results.

Take, for example, Grace Hopper, famous both as a computer programmer and as the person who coined the term computer bug. If you were to run a search with "Grace Hopper" as the primary search and overlay a search for COBOL on the domain of the first result returned, you'd find the following three links at the top of the results page:

GHC - 2004

... Website: ... and on making machines understand ordinary language 

instructions led ultimately to the development of the business language COBOL. ...

GHC - 2004

... Website: ... and on making machines understand ordinary language 

instructions led ultimately to the development of the business language COBOL. ...


... uspers-h/g-hoppr.htm. Whitman College:

cobol.html. Yale University (The Ada Project): http ...

You could also do a primary search for a person ("Stan Laurel") and a secondary search for another person ("Oliver Hardy"). Or search for a person, followed by their corporate affiliation.

Don't try doing a link: search with this hack. The link: special syntax doesn't work with any other special syntaxes, and this hack relies upon inurl:.

2.8.1. The Code

Save the code as goolucky.cgi, a CGI script on your web server ["How to Run the Hacks" in the Preface] or that of your Internet service provider.


# goolucky.cgi

# Gleans the domain from the first (read: top) result returned, allows

# you to overlay another query, and returns the results, and so on...

# goolucky.cgi is called as a CGI with form input.


# Your Google API developer's key.

my $google_key='insert key here';


# Location of the GoogleSearch WSDL file.

my $google_wdsl = "./GoogleSearch.wsdl";


use strict;


use SOAP::Lite;

use CGI qw/:standard/;


# Create a new SOAP instance.

my $google_search = SOAP::Lite->service("file:$google_wdsl");


# If this is the second time around, glean the domain.

my $query_domain = param('domain') ? "inurl:" . param('domain') : '';

my $results = $google_search ->


    $google_key, param('query') . " $query_domain", 0, 10,

    "false", "", "false", "", "latin1", "latin1"



# Set domain to the results of the previous query.

param('domain', $results->{'resultElements'}->[0]->{'URL'});

param('domain', param('domain') =~ m#://(.*?)/#);



  header( ),

  start_html("I'm Feeling VERY Lucky"),

  h1("I'm Feeling VERY Lucky"),

  start_form( ),

  'Query: ', textfield(-name=>'query',

  -default=>'"Grace Hopper"'),

  ' &nbsp; ',

  'Domain: ', textfield(-name=>'domain'),

  ' &nbsp; ',

  submit(-name=>'submit', -value=>'Search'),

  p( ),



foreach (@{$results->{'resultElements'}}) {

  print p(

    b($_->{title}), br( ),

    a({href=>$_->{URL}}, $_->{URL}), br( ),






  end_form( ),

  end_html( );

Replace insert key here with your Google API key.

2.8.2. Running the Hack

Point your web browser at the CGI script, goolucky.cgi. The script pops up a form in which you should enter a query and a domain within which to search; hit the Search button when you're ready to run your query.

2.8.3. Hacking the Hack

You can also run this hack so that it only uses one query. For example, do a search with Query A, and the search grabs the domain from the first result. Then run another search, again using Query A, but restrict your results to the domain that was grabbed in the first search. This is handy when you're trying to get information on one set of keywords, instead of trying to link two different concepts. Figure 2-3 illustrates the I'm Feeling VERY Lucky search.

Figure 2-3. I'm Feeling VERY Lucky search

    Previous Section  < Day Day Up >  Next Section