Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 73 Getting Random Results (On Purpose)

figs/expert.giffigs/hack73.gif

Surfing random pages can turn up some brilliant finds.

Why would any researcher worth her salt be interested in random pages? While surfing random pages isn't what one might call a focused search, you'd be surprised at some of the brilliant finds you'd never have come across otherwise. I've loved random page generators associated with search engines ever since discovering Random Yahoo! Link (http://random.yahoo.com/bin/ryl) and thought creating such a thing to work with the Google API might prove interesting, useful even.

73.1 The Code

What this code does is search for a random number between 0 and 99999 (yes, you can search for 0 with Google) in addition to a modifier pulled from the @modifiers array. To generate the random page, you don't, strictly speaking, need something from the modifer array. However, it helps make the page selection even more random.

With the combination of a number between 0 and 99999 and a modifier from the @modifiers array, Google will get a list of search results, and from that list you'll get a "random" page. You could go higher with the numbers if you wanted, but I wasn't sure that this hack would consistently find numbers higher than 99999. (Zip Codes are five digits, so I knew a five-digit search would find results more often than not.)

73.2 The Code

#!/usr/local/bin/perl
# goorandom.cgi
# Creates a random Google query and redirects the browser to
# the top/first result.
# goorandom.cgi is called as a CGI without any 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;

# a list of search modifiers to be randomly chosen amongst for
# inclusion in the query
my @modifiers = ( "-site:com", "-site:edu", "-site:net",
                  "-site:org", "-site:uk", "-file:pdf", );

# picking a random number and modifier combination
my $random_number  = int( rand(99999) );
my $random_modifier = $modifiers[int( rand( scalar(@modifiers) ) )];

# Create a new SOAP object
my $google_search  = SOAP::Lite->service("file:$google_wdsl");

# Query Google
my $results = $google_search -> 
  doGoogleSearch(
    $google_key, "$random_number $random_modifier",
    0, 1, "false", "",  "false", "", "latin1", "latin1"
  );

# redirect the browser to the URL of the top/first result
print "Location: $results->{resultElements}->[0]->{URL}\n\n";

73.3 Running the Hack

This hack runs as a CGI script; invoke it from your preferred web browser.

73.4 Hacking the Hack

There are a couple of ways to hack this hack.

73.4.1 Modifying the modifiers

You'll notice each modifier in the @modifier array is preceded by a negative ("exclude this"). You can, of course, add anything you wish, but it's highly recommended you keep to the negative theme; including something like "computers" in the list gives you a chance—a slight chance, but a chance nevertheless—of coming up with no search results at all. The hack randomly excludes domains; here are a few more possibilities:

 -intitle:queryword
 -inurl:www
 -inurl:queryword
 -internet
 -yahoo
 -intitle:the

If you want to, you could create modifiers that use OR (|) instead of negatives, and then slant them to a particular topic. For example, you could create an array with a medical slant that looks like this:

(medicine | treatment | therapy)
(cancer | chemotherapy | drug) 
(symptoms | "side effects") 
(medical | research | hospital) 
(inurl:edu | inurl:gov )

Using the OR modifier does not guarantee finding a search result like using a negative does, so don't narrow your possible results by restricting your search to the page's title or URL.

73.4.2 Adding a touch more randomness

The hack, as it stands, always picks the first result. While its already highly unlikely you'll ever see the same random page twice, you can achieve a touch more randomness by choosing a random returned result. Take a gander at the actual search itself in the hack's code:

my $results = $google_search -> 
  doGoogleSearch(
    $google_key, "$random_number $random_modifier",
    0, 1, "false", "",  "false", "", "latin1", "latin1"
  );

You see that 0 at the beginning of the fourth line? That's the offset: the number of the first result to return. Change that number to anything between 0 and 999, and you'll shift the results returned by that number—assuming, of course, that the number you choose is smaller than the number of results for the query at hand. For the sake of just about guaranteeing a result, it's probably best to stick to numbers between 0 and 10. How about randomizing the offset? Simply alter the code as follows (changes in bold):

...
# picking a random number, modifier, and offset combination
my $random_number  = int( rand(99999) );
my $random_modifier = $modifiers[int( rand( scalar(@modifiers) ) )];
my $random_offset = int( rand(10) );
...
my $results = $google_search -> 
  doGoogleSearch(
    $google_key, "$random_number $random_modifier",
    $random_offset, 1, "false", "",  "false", "", "latin1", "latin1"
  );
  
... 
    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
    Chapter 6. Google Web API Applications
    6.1 Hacks #60-85
    6.2 The Ingenuity of Millions
    6.3 Learning to Code
    6.4 What You'll Find Here
    6.5 Finding More Google API Applications
    6.6 The Possibilities Aren't Endless, but They're Expanding
    Hack 60 Date-Range Searching with a Client-Side Application
    Hack 61 Adding a Little Google to Your Word
    Hack 62 Permuting a Query
    Hack 63 Tracking Result Counts over Time
    Hack 64 Visualizing Google Results
    Hack 65 Meandering Your Google Neighborhood
    Hack 66 Running a Google Popularity Contest
    Hack 67 Building a Google Box
    Hack 68 Capturing a Moment in Time
    Hack 69 Feeling Really Lucky
    Hack 70 Gleaning Phonebook Stats
    Hack 71 Performing Proximity Searches
    Hack 72 Blending the Google and Amazon Web Services
    Hack 73 Getting Random Results (On Purpose)
    Hack 74 Restricting Searches to Top-Level Results
    Hack 75 Searching for Special Characters
    Hack 76 Digging Deeper into Sites
    Hack 77 Summarizing Results by Domain
    Hack 78 Scraping Yahoo! Buzz for a Google Search
    Hack 79 Measuring Google Mindshare
    Hack 80 Comparing Google Results with Those of Other Search Engines
    Hack 81 SafeSearch Certifying URLs
    Hack 82 Syndicating Google Search Results
    Hack 83 Searching Google Topics
    Hack 84 Finding the Largest Page
    Hack 85 Instant Messaging Google
    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