in reply to HTML Parser print text

Ok, so based on your replies above, given:
<HTML> <title>My Page</title> </head> <body> <center> <h1>Brand.com Production Instances</h1> <br> <table border=1> <tr><td></td><td><b>&nbsp;Service &nbsp;&nbsp;</b></td><td><b>Instance +&nbsp;</b></td> <tr><td align="right">1</td><td>&nbsp;app2<br></td><td>&nbsp;prd-1</td +><td> </td> </tr> <tr><td align="right">2</td><td>&nbsp;app2 &nbsp;<br></td><td>&nbsp;pr +d-2</td><td> </td></tr> <tr><td align="right">3</td><td>&nbsp;app3<br></td><td>&nbsp;prd-1</td +><td>
etc etc

you want to print out the text in the <td> tags that have align="right" as an attribute.

This code will do that:

#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTML::Parser; # Create instance my $p = HTML::Parser->new(api_version => 3, marked_sections => 1, unbroken_text => 1, start_h => [\&start, "tagname, attr"], text_h => [\&text, 'text'], ); # Start parsing the following HTML file $p->parse_file("testpage.html"); my $get_next_text = 0; sub start{ # Execute when start tag is encountered my ($tagname,$attr) = @_; if ($tagname eq 'td' && exists $attr->{align} && $attr->{align} eq + 'right'){ $get_next_text = 1; } else { $get_next_text = 0; } } sub text { my $text = shift; print "$text\n" if $get_next_text; }

What it does is this:

  1. Set up HTML::Parser so that for each start tag &start gets called with as arguments the tag name ("td" or something else) followed by the attributes as a hash-ref) and that for all text parts &text gets called with the text as the argument.

    Note that a start tag is ANY tag that doesn't begin with </ - so <p> is a start tag and <td> is a start tag, but </p> is not. A "text" part is anything that is not a tag.

  2. Test in &start if the current tag is a <td> with an align="right" attribute. If yes: set $get_next_text to true. if no: set $get_next_text to false.

  3. Test in &text if the previous tag was a <td align="right"> (via the $get_next_text variable). If yes, print, otherwise do nothing.
Hope this clears it up :-)

Joost.

Replies are listed 'Best First'.
Re^2: HTML Parser print text
by Vanquish (Novice) on Jul 07, 2004 at 21:13 UTC
    Great Works
    Thanks Alot
    Have a nice Day
    MQ