Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: HTML::TokeParser help - parsing headlines

by Ovid (Cardinal)
on Mar 07, 2004 at 04:21 UTC ( [id://334576]=note: print w/replies, xml ) Need Help??


in reply to HTML::TokeParser help - parsing headlines

If you switch to HTML::TokeParser::Simple, I think you'll be happy with how much clearer the logic is.

use strict; use HTML::TokeParser::Simple; use LWP::Simple; use URI; my $url = 'http://www.reuters.com/newsEarlierArticles.jhtml?type=busin +essNews'; my $stream = HTML::TokeParser::Simple->new(\get($url)) || die "Couldn't read $url: $!"; while(my $token = $stream->get_token) { next unless $token->is_start_tag('td') and ($token->return_attr('class') || '') eq 'earlyHeadline'; my $next = $stream->get_token; if ($next->is_start_tag('a')) { print URI->new_abs($next->return_attr('href'), $url), "\n"; } }

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: Re: HTML::TokeParser help - parsing headlines
by perleager (Pilgrim) on Mar 07, 2004 at 09:21 UTC
    Hey,

    I adjusted my code correctly to extract the urls from the reauters headlines.

    However, when printing out the urls it looks like:

    http://www.reuters.com/newsArticle.jhtml;jsessionid=1GRGO0RUSCREMCRBAE +0CFFA?type=businessNews&storyID=4512094告on=news http://www.reuters.com/newsArticle.jhtml;jsessionid=1GRGO0RUSCREMCRBAE +0CFFA?type=businessNews&storyID=4512054告on=news http://www.reuters.com/newsArticle.jhtml;jsessionid=1GRGO0RUSCREMCRBAE +0CFFA?type=businessNews&storyID=4512041告on=news
    If you copy and paste one of those url's, it will bring you to a blank reuters template, part being because at the end part of the url where it has "告on=news", should really be "'&'section=news".

    Somehow its translating the "'&'section=news" into "告on=news".

    Could it be because I'm using MIME-Base32 and not MIME-Base64 module? --I'm on a Windows machine.

    Adjusted code:
    #!/usr/bin/perl -w use strict; use HTML::TokeParser; use LWP::Simple; use URI; print "Content-type: text/html\n\n"; my $filename = 'temp.html'; open FH, ">$filename"; print FH get("http://www.reuters.com/newsEarlierArticles.jhtml?type=bu +sinessNews"); close FH; my $stream = HTML::TokeParser->new($filename) || die "Couldn't read HTML file $filename: $!"; while(my $token = $stream->get_token) { if ($token->[0] eq 'S' and $token->[1] eq 'td' and ($token->[2]{'class'} || '') eq 'earlyHeadline') { my(@next) = ($stream->get_token); if ($next[0] and $next[0][0] eq 'S' and $next[0][1] eq 'a' and defined + $next[0][2]{'href'} ) { #early headline found for business section/grab a href portion print URI->new_abs($next[0][2]{'href'}, 'http://www.reuter +s.com/'), "\n"; } } }
    Thank you,
    Anthony

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://334576]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-03-28 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found