Sunday, January 08, 2006

The blog contents.

This code builds a Yahoo! Search Web Services request URL using the keyword passed to it when the script is run. Then it parses the response and prints it out in a readable format. Save the following code to a file named yahoo_search.pl:

#!/usr/bin/perl
# yahoo_search.pl
# Accepts a search term and shows the top results.
# Usage: [root localhost /root]./yahoo_search.pl
#
# You can create an AppID, and read the full documentation
# for Yahoo! Web Services at http://developer.yahoo.net/
use strict;
use LWP::Simple;
use XML::Simple;
# Set your unique Yahoo! Application ID
my $appID = "insert your app ID";

# But first, we need an active Yahoo! subscription before proceeding.
# Grab the incoming search query
my $query = join(' ', @ARGV) or die "Usage: yahoo_search.pl \n";
# Construct a Yahoo! Search Query with only required options
my $language = "en";
my $req_url = "http://api.search.yahoo.com/";
$req_url .= "WebSearchService/V1/webSearch?";
$req_url .= "appid=$appID";
$req_url .= "&query=$query";
$req_url .= "&language=$language";
# Make the request
my $yahoo_response = get($req_url);
# Parse the XML
my $xmlsimple = XML::Simple->new( );
my $yahoo_xml = $xmlsimple->XMLin($yahoo_response);
# Initialize results counter
my $i;
# Loop through the items returned, printing them out
foreach my $result (@{$yahoo_xml->{Result}}) {
$i++;
my $title = $result->{Title};
my $summary = $result->{Summary};
my $url = $result->{Url};
print "$i. $title\n$summary\n$url\n\n";
}

This code is however goofed up. Don't know why, but somethings are still not the way as expected.
Now, what is the prime catch for Yahoo! here?
Nothin' as mentioned in code, only ya have to register an account at Yahoo!
Yahoo! API's are good then Google's SOAP modules, but who knows what can happen next!
Security or privacy policy: Don't ask!




All source code contents of this blog are released in GPL unless mentioned explicitely.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home