Read Rss from xml-url

Go To StackoverFlow.com

0

Here is my code

#!usr/bin/env perl
# Setup includes
use strict;
use XML::RSS;
use LWP::Simple;
# Declare variables for URL to be parsed
my $url2parse;
# Get the command-line argument
my $arg = shift;
# Create new instance of XML::RSS
my $rss = new XML::RSS;
# Get the URL, assign it to url2parse, and then parse the RSS content
$url2parse = get($arg);
die "Could not retrieve $arg" unless $url2parse;
$rss->parse($url2parse);
# Print the channel items
foreach my $item (@{$rss->{'items'}}) {
     next unless defined($item->{'title'}) && defined($item->{'link'});
     print "<li><a href=\"$item->{'link'}\">$item->{'title'}</a><BR>\n";
}

when I typed in %perl myRss.pl http://www.nytimes.com/services/xml/rss/userland/Education.xml, the console runs a while and returns "Could not retrieve http://www.engadget.com/rss.xml at myRss.pl line 14." Where does my code go wrong?

2012-04-04 01:09
by rain zwr


0

Well, the output really says it all: at myRss.pl line 14

die "Could not retrieve $arg" unless $url2parse;

This means LWP::Simple failed to fetch the URL you provided. If you want to know why it fails, you should probably switch to LWP::UserAgent. Something like this shoud tell you more:

use LWP::UserAgent;
sub get {
    my $url = shift;
    my $ua = LWP::UserAgent->new();
    my $res = $ua->get($url);

    die ("Could not retrieve $url: " . $res->status_line) unless($res->is_success);
    return $res->content;
}

Simply remove LWP::Simple from your code and you can use your "custom get()" subroutine.

2012-04-04 01:50
by Sebastian Stumpf