使用 XML 解析器会有所帮助,这里使用perl
and进行测试XML::Twig
。使其适应您的需求。
内容script.pl
:
#!/usr/bin/env perl
use warnings;
use strict;
use XML::Twig;
my $twig = XML::Twig->new(
twig_handlers => {
'title' => \&extract_text,
'description' => \&extract_text,
},
)->parsefile( shift );
sub extract_text {
my ($t, $e) = @_;
printf qq|%s\n=================\n|, $e->tag;
printf qq|%s\n\n|, $e->text;
}
像这样运行它:
curl --silent "http://rss.slashdot.org/Slashdot/slashdot" | perl script.pl -
对于每对标题和描述,这会产生类似以下内容:
title
=================
Proof-of-Concept Port of XBMC to SDL 2.0 and Wayland
description
=================
hypnosec wrote in with news that XBMC has ...