Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   A PERL take on Google Suggest Data (http://www.go4expert.com/articles/perl-google-suggest-data-t448/)

naveen 30Sep2005 12:33

A PERL take on Google Suggest Data
 
Well this has been out for quite sometime now, so i thought to share with all out here. Heres a PERL script demonstrating how to retrieve the data from the Google Suggest Drop Down Autocomplete and use it as you please! This was developed by John Bokma , but worked in command line only. So i have tweaked it a bit for browser interface and also kept the sorted output instead of both sorted and unsorted ones as in the original script. After placing the script in your cgi-bin, just call the script in your browser after adding a "?" followed by whatever you want to search.
Enjoy
Naveen

google_suggest.pl
Code: PERL

#!/usr/bin/perl

use strict;
use warnings;

use URI::Escape;
use LWP::UserAgent;

print "Content-type: text/html\n\n";

unless ( @ARGV ) {

    print "<center><b>Usage:</b> In the browser, type the gsuggest.pl URL followed by   <b><i>?whatever_you_want_to_search</i></b>\n";
    exit( 1 );
}

my $url = 'http://www.google.com/complete/search?js=true&qu=' .
    uri_escape( join ' ' => @ARGV );

my $ua = LWP::UserAgent->new( agent => 'Mozilla/5.0' );
my $response = $ua->get( $url );

$response->is_success or
    die "$url: ", $response->status_line;

my $content = $response->content;

my ( $element, $array1, $array2 ) = $content =~
    /"(.+?)".+?Array\("(.+?)"\).+?Array\("(.+?)"\)/;

unless ( defined $array1 ) {

    print "No results\n";
    exit;
}

my @suggestions = split /", "/, $array1;

$array2 =~ s/ results?//g;
$array2 =~ s/&nbsp;//g;
my @results = split /", "/, $array2, -1;

my @pairs = map { [ sprintf ( "%12s", shift @results ) => $_ ] } @suggestions;

print "<center><b>Sorted Results of the query <u><i>@ARGV</u></i> from <a href='http://www.google.com/webhp?complete=1&hl=en'>Google Suggest</a>:</b><br><br>";
print "@$_<br>" for sort { $b->[0] cmp $a->[0] } @pairs;
print "<br><br><i><b>(Replace '@ARGV' in the browser with the string you want results of)</i></b><br><br><br><br>&copy; Joh&#110&nbspBokm&#97<br><b>Modifie&#100&nbsp;By</b>&nbspNaveen&nbsp;Gupt&#97";



All times are GMT +5.5. The time now is 20:24.