Okay I know stuff like this has been done before (Grab weather from yahoo, etc.) but I thought I'd post it anyway for some feedback. This script grabs the major headlines off of http://news.yahoo.com/ and prints them in their own table. Works great by calling it via an exec SSI or a system call in perl. I could have probably gotten away with using just LWP::Simple and I probably didn't have to use HTML::TokeParser but it makes it a little more custamizable, i.e. it'd be easy to strip the image tags etc.
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
use HTML::TokeParser;
### Main Program ###
my $ua = LWP::UserAgent->new;
my $rq = HTTP::Request->new('GET', 'http://news.yahoo.com');
my $r = $ua->request($rq);
my ($content, $parser, $token, $token2);
if ($r->is_success) { # parse the contents of the page
$content = $r->content;
$parser = HTML::TokeParser->new(\$content);
while ($token = $parser->get_token) {
if ($token->[0] eq 'T' && $token->[1] =~ /Top (\s+|\n)Story/)
+{
print "<table border=0 cellspacing=0 cellpadding=3 width=\
+"100%\"><tr bgcolor=dcdcdc><td><font face=\"arial,helvetica\"><b>";
print $token->[1];
while ($token2 = $parser->get_token) {
if ($token2->[0] eq 'S') {
print $token2->[4];
} elsif ($token2->[0] eq 'E') {
print $token2->[2];
} else {
print $token2->[1];
}
last if $token2->[1] =~ /More\s+Health\s+Headlines/;
}
}
}
print "</b></a></font></td></tr></table>";
}
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.