1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

A PERL take on Google Suggest Data

Discussion in 'Perl' started by naveen, Sep 30, 2005.

  1. naveen

    naveen New Member

    Joined:
    Jun 2, 2005
    Messages:
    39
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Calcutta, India
    Home Page:
    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:
    #!/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";
    
     

Share This Page