Scattersearch with Yahoo! and Google

Sometimes, illuminating results can be found when scraping from one site and feeding the results into the API of another. With scattersearching, one can narrow down the most popular related results, as suggested by Yahoo! and Google

Yahoo! has a "Related searches" feature, where you enter a search term and get a list of related terms under the search box, if any are available. This hack scrapes those related terms and performs a Google search for the related terms in the title. It then returns the count for those searches, along with a direct link to the results. Aside from showing how scraped and API-generated data can live together in harmony, this hack is good to use when you're exploring concepts; for example, you might know that something called Pokemon exists, but you might not know anything about it. You'll get Yahoo!'s related searches and an idea of how many results each of those searches generates in Google. From there, you can choose the search terms that generate the most results or look the most promising based on your limited knowledge, or you can simply pick a road that appears less traveled.

Save the following code to a file called scattersearch.pl.


#!/usr/bin/perl -w # # Scattersearch -- Use the search suggestions from # Yahoo! to build a series of intitle: searches at Google. use strict; use LWP; use SOAP::Lite; use CGI qw/:standard/; # Get our query, else die miserably. my $query = shift @ARGV; die unless $query; # Your Google API developer's key. my $google_key = 'insert key here'; # Location of the GoogleSearch WSDL file. my $google_wdsl = "./GoogleSearch.wsdl"; # Search Yahoo! for the query. my $ua = LWP::UserAgent->new; my $url = URI->new('http://search.yahoo.com/search'); $url->query_form(rs => "more", p => $query); my $yahoosearch = $ua->get($url)->content; $yahoosearch =~ s/[\f\t\n\r]//isg; # And determine if there were any results. $yahoosearch =~ m!Also try:(.*?) !migs; die "Sorry, there were no results!\n" unless $1; my $recommended = $1; # Now, add all our results into # an array for Google processing. my @googlequeries; while ($recommended =~ m!(.*?)!mgis) { my $searchitem = $1; $searchitem =~ s/nobr<[^>]*>\///g; print "$searchitem\n"; push (@googlequeries, $searchitem); } # Print our header for the results page. print join "\n", start_html("ScatterSearch"); h1("Your Scattersearch Results"), p("Your original search term was '$query'"), p("That search had " . scalar(@googlequeries). " recommended terms."), p("Here are result numbers from a Google search"), CGI::start_ol( ); # Create our Google object for API searches. my $gsrch = SOAP::Lite->service("file:$google_wdsl"); # Running the actual Google queries. foreach my $googlesearch (@googlequeries) { my $titlesearch = "allintitle:$googlesearch"; my $count = $gsrch->doGoogleSearch($google_key, $titlesearch, 0, 1, "false", "", "false", "", "", ""); my $url = $googlesearch; $url =~ s/ /+/g; $url =~ s/\"/%22/g; print li("There were $count->{estimatedTotalResultsCount} ". "results for the recommended search $googlesearch"); } print CGI::end_ol( ), end_html;


Running the Hack
This script generates an HTML file, ready for you to upload to a publicly accessible web site. If you want to save the output of a search for siamese to a file called scattersearch.html in your Sites directory, run the following command ["How to Run the Hacks" in the Preface]:


% perl scattersearch.pl "siamese" > ~/Sites/scattersearch.html


Hacking the Hack
You have two choices: you can either hack the interaction with Yahoo! or expand it to include something in addition to or instead of Yahoo! itself. Let's look at Yahoo! first. If you take a close look at the code, you'll see we're passing an unusual parameter to our Yahoo! search results page:
$url->query_form(rs => "more", p => $query);
The rs=>"more" part of the search shows the related search terms. Getting the related search this way will show up to 10 results. If you remove that portion of the code, you'll get roughly four related searches when they're available. That might suit you if you want only a few, but perhaps you want dozens and dozens! In that case, replace more with all.

No comments: