Hi all.
I am cultivating an interest in scraping RSS feeds and the following program is my first
real attempt:
#!/usr/bin/perl -w
use strict;
use XML::Parser;
use LWP::Simple;
my $feed;
open( FH, ">feed.xml") or die "Error: $!\n";
$feed = get("http://rss.news.yahoo.com/rss/science");
print FH $feed;
my $parser = new XML::Parser ( Handlers => {
Start => \&hdl_start,
End => \&hdl_end,
Char => \&hdl_char,
} );
+
$parser->parsefile("feed.xml");
sub hdl_start {
my ($p, $ele, %attribs) = @_;
$attribs{'string'} = '';
$feed = \%attribs;
}
sub hdl_end {
+
my ($p, $ele) = @_;
display_feed($feed) if $ele eq 'title';
display_feed($feed) if $ele eq 'link';
}
sub hdl_char {
my ($p, $str) = @_;
no strict 'refs';
$feed->{'string'} .= $str;
}
sub display_feed {
my $attribs = shift;
$attribs =~ s/\n//g;
print "$attribs->{'string'}\n\n";
}
The last title tag doesn't generate a corresponding link and the following message appears once the feed is displayed:
no element found at line 594, column 12, byte 45056 at C:/Perl/site/lib/XML/Parser.pm line 185
Ideally, the program would display the feed in html formatted links with the title directly above them for easy navigation to the story in question.
I've used
XML::RSS before but I decided to experiment with an additional module.
Thanks,
~Katie
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.