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 (http://search.cpan.org/~wumpus/WebService-Blekko-1.00_07/lib/WebService/Blekko.pm), 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
my $query='perl programming';
my $results=$response->content; die unless $response->is_success;
print "$i $link\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 email@example.com.