Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: problem HTML::FormatText::WithLinks::AndTables

by DanEllison (Scribe)
on Dec 07, 2012 at 17:57 UTC ( [id://1007810]=note: print w/replies, xml ) Need Help??


in reply to problem HTML::FormatText::WithLinks::AndTables

I don't have any experience with HTML::FormatText, but LWP and HTML::TreeBuilder are rather dear to my heart. Try out the following code, I think it will get you closer to what you want. I just put the links in line with the text they were associated to, but you could easily print a footnote an push them into an array.
use strict; use LWP::UserAgent; use HTML::TreeBuilder; my $ua = LWP::UserAgent->new; my $page = $ua->LWP::UserAgent::get("http://www.databasefootball.com/b +oxscores/scheduleyear.htm?yr=1985&lg=nfl", 'User-Agent'=>'Mozilla/5.0'); my $tree = HTML::TreeBuilder->new; $tree->parse_content($page->decoded_content); #$tree->dump; foreach my $table ($tree->look_down('_tag', 'table')) { print "###Table###\n"; foreach my $row ($table->look_down('_tag', 'tr', sub { $_[0]->look +_up('_tag', 'table') == $table; })) { if ($row->as_text =~ / at /) { my $c = 0; foreach my $col ($row->look_down('_tag', qr/^t[dh]$/, sub +{ $_[0]->look_up('_tag', 'tr') == $row; })) { if ($c++) { print " "; } printf "%s", $col->as_text; if ($col->look_down('href', qr/./)) { printf " [%s]", $col->look_down('href', qr/./)->at +tr('href'); } } print "\n"; } else { printf "%s\n", $row->as_text; } } } exit;
When looking for rows and columns, I perform a look_up nexted inside my look_down. This may not be an issue for this webpage, but I deal with a lot of nested tables on my websites, and this eliminates processing a row within a nested table.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-25 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found