|
Free Open Book
Google Hacks |
Hack 73 Getting Random Results (On Purpose)
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 CodeWhat 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 HackThis hack runs as a CGI script; invoke it from your preferred web browser. 73.4 Hacking the HackThere are a couple of ways to hack this hack. 73.4.1 Modifying the modifiersYou'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 randomnessThe 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" ); ...
|
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |