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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.