XML::RSS::Parser 和 Facebook RSS feed ...
我需要一个子例程来解析传递给它的“任何”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";
Wired Facebook Feed 的结果
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 feed 在 ie android RSS 阅读器中运行良好。
I need a subroutine which should parse "any" RSS feed passed to it. I was using XML::RSS:Parser a few times already for some RSS feed but it does not work with Facebook.
Example code:
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";
Result with Wired Facebook Feed
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.
I think that XML::RSS::Parsers does not get correct namespace from the root element and nothing works after that. Ideas how to solve this?
I could use XML::Simple or something similar to parse Facebook's RSS but I want one parser for all rss feeds.
I am going to hack around XML/RSS/Parser.pm to find the reason but it's not the solution to change package only for facebook. And this facebook feed works well in i.e. android rss reader.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定收到有效的网络反馈吗?在尝试解析返回的内容之前,您可能应该检查
$res->is_error
。当我刚刚尝试你的代码时,我收到了 500 错误。另外,您还需要 Atom feed (
&format=atom10
)。您确定要使用 RSS 解析器来解析它吗?我在 XML::RSS::Parser 文档 其中提到它适用于 Atom feed。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.