Skip to content

Instantly share code, notes, and snippets.

Created Oct 10, 2010
What would you like to do?
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use JSON;
use Getopt::Long;
use URI::Escape qw( uri_escape_utf8 );
# options
my $query;
my $titles;
my $count = 8;
GetOptions("query|q=s" => \$query,
"title|t" => \$titles,
"count|c=i" => \$count);
if (!$query) {
print STDERR $0.": No query given. Use the --query (-q) option.\n";
exit 0;
my $url = "".uri_escape_utf8($query)."&rsz=".$count;
my $ua = LWP::UserAgent->new();
my $body = $ua->get($url);
# process the json string
my $json = from_json($body->decoded_content);
# have some fun with the results
my $i = 0;
foreach my $result (@{$json->{responseData}->{results}}){
print $result->{url};
if ($titles) {
print "\t".$result->{titleNoFormatting};
print "\n";
if (!$i) {
print STDERR $0.": no results for query ".$query."\n";
exit 1;
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment