Monday, May 14, 2012

A Quick Look at the Blekko Search API

While less of a household name than some of the other search giants, the search engine Blekko also puts out a search API that can be incorporated into a Perl script.  There is a CPAN module already available that serves as a wrapper between the Blekko API and your Perl application (, but in this little code snippet we will focus on direct access to the Blekko API.  One option that direct access of the API allows for that the CPAN module does not is retrieval of results in an RSS format. 

The Blekko API works via a GET request and requires at a minimum that the query of interest be appended to the URL as follows:

While this will work to retrieve results in an HTML format, slashtags can be specified to retrieve results in a different format.  /json will result in JSON results being returned, while /rss will result in the results being returned in an RSS format.  Other slashtags such as /ps can also be applied to further control the returned results.  The /ps slashtag controls the number of returned results.  The demo code below will make use of the /rss slashtag to retrieve XML results which will parsed with the XML::LibXML module in order to extract the URL and description of each result.  The sample Perl code is as follows:

# Copyright 2012- Christopher M. Frenz
# This script is free software - it may be used, copied, redistributed, and/or modified
# under the terms laid forth in the Perl Artistic License

use LWP;
use XML::LibXML;
use strict;

my $ua=LWP::UserAgent->new();
my $query='perl programming';
my $url="$query+/rss";
my $response=$ua->get($url);
my $results=$response->content; die unless $response->is_success;

my $parser=XML::LibXML->new;
my $domtree=$parser->parse_string($results);
my  @Records=$domtree->getElementsByTagName("item");
my $i=0;
   my $link=$Records[$i]->getChildrenByTagName("link");
   print "$i $link\n";
   my $description=$Records[$i]->getChildrenByTagName("description");
   print "$description\n\n";

Note, that at the time of this writing Blekko does not enforce the need for an API key, so it is not included in the example.  However, they do issue API keys and thus may enforce their use in the future.  Blekko API keys can be obtained from 

No comments: