1

我需要一个子程序来解析传递给它的“任何”RSS 提要。我已经多次使用 XML::RSS:Parser 来处理一些 RSS 提要,但它不适用于 Facebook。

示例代码:

use LWP::Simple;
use XML::RSS::Parser;

my $url = join '', @ARGV;
die "No URL passed" if !$url;

# facebook does not accept default LWP user agent
my $ua = LWP::UserAgent->new(agent => 'iGoogleBot');
my $res = $ua->get($url);
my $content = $res->decoded_content;

my $parser = XML::RSS::Parser->new;
my $feed = $parser->parse_string($content) or die $parser->errstr;
print "COUNT: ".$feed->item_count."\n";

有线 Facebook 提要的结果

xf@serv:/tmp$ ./rss.pl 'https://www.facebook.com/feeds/page.php?id=19440638720&format=atom10'
Can't call method "contents" on an undefined value at /usr/local/share/perl/5.10.1/XML/RSS/Parser.pm line 122.

我认为 XML::RSS::Parsers 没有从根元素获得正确的命名空间,之后没有任何效果。想法如何解决这个问题?

我可以使用 XML::Simple 或类似的东西来解析 Facebook 的 RSS,但我想要一个解析器来处理所有 rss 提要。

我将破解 XML/RSS/Parser.pm 以找到原因,但这不是仅为 facebook 更改包的解决方案。这个 facebook 提要在 ie android rss reader 中运行良好。

4

1 回答 1

2

Are you sure that you're getting a valid web feed back? You should probably check $res->is_error before trying to parse the content that you get back. When I just tried your code, I got a 500 error.

Also, you're asking for an Atom feed (&format=atom10). Are you sure that you want to parse that with an RSS parser? I can't see anything in the XML::RSS::Parser documentation that mentions it working for Atom feeds.

于 2011-12-01T14:23:36.310 回答