Vanquish has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am trying to print out plain text from the tag (TD) and i also want to add attributes. Help needed Thanks MQ
#!/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_rtn, 'tag','event'], text_h => [\&text_rtn, 'text'], end_h => [\&end_rtn, 'tag']); # Start parsing the following HTML file $p->parse_file("testpage.html"); sub start{ # Execute when start tag is encountered my ($tag, $text, $event) = @_; if ($tag =~ /td/){ print "\t$text \n"; } }

Replies are listed 'Best First'.
Re: HTML Parser print text
by Joost (Canon) on Jul 07, 2004 at 21:01 UTC
    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.

      Great Works
      Thanks Alot
      Have a nice Day
      MQ
Re: HTML Parser print text
by Joost (Canon) on Jul 07, 2004 at 18:14 UTC
      Thanks for reply I need plain text embeded in tags with attriutes. My code wont print any output. testpage.html is nothing but simple hello world page. Thanks again
        testpage.html CAN'T be a simple "Hello World" page, because, for my definition of simple, that would look like:
        <html> <head> <title>Some title</title> </head> <body> Hello, World </body> </html>
        And would not contain any <td> tags. Besides, I still don't know WHAT the tags are that you want around it (or do you want the plain text in an attribute? I've no clue)

        So please:

        Show the input file. Completely.

        Show the code. Completely.

        Show your intended output. Completely. And tell us a bit about why it should be exactly that output.