0

これが私のコードです

#!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";
}

%perl myRss.pl http://www.nytimes.com/services/xml/rss/userland/Education.xmlと入力すると、コンソールがしばらく実行 され、「 http://www.engadgetを取得できませんでした。 com/rss.xmlの myRss.pl 行 14." 私のコードはどこで間違っていますか?

4

1 に答える 1

0

まあ、出力は本当にそれをすべて言います: myRss.pl 行 14

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

これは、LWP::Simple が提供された URL の取得に失敗したことを意味します。失敗する理由を知りたい場合は、おそらく に切り替える必要がありますLWP::UserAgent。このようなものはあなたにもっと教えてくれるはずです:

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;
}

コードから LWP::Simple を削除するだけで、「カスタム get()」サブルーチンを使用できます。

于 2012-04-04T01:50:28.693 に答える