Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
830 views
in Technique[技术] by (71.8m points)

html - Extract Table Contents using Perl

I am trying to extract table content from a html file using HTML::TableExtract. My problem is my html file is structured in the following way:

<!DOCTYPE html>
<html>
<body>

    <h4>One row and three columns:</h4>

    <table border="1">
      <tr>
        <td>
        <p> 100 </p></td>
        <td>
        <p> 200 </p></td>
        <td>
        <p> 300 </p></td>
        </tr>
      <tr>
        <td>
        <p> 100 </p></td>
        <td>
        <p> 200 </p></td>
        <td>
        <p> 300 </p></td>
        </tr>
    </table>
</body>
</html>

Because of this structure, my output looks like:

   100|

   200|

   300|

   400|

   500|

   600|

Instead of what I want:

   100|200|300|
   400|500|600|

Can you please help? Here is my perl code

use strict;
use warnings;
use HTML::TableExtract;

my $te = HTML::TableExtract->new();
$te->parse_file('Table_One.html');

open (DATA2, ">TableOutput.txt")
    or die "Can't open file";

foreach my $ts ($te->tables()) {

    foreach my $row ($ts->rows()) {

        my $Final = join('|', @$row );
    print DATA2 "$Final";
    }
}
close (DATA2);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
sub trim(_) { my ($s) = @_; $s =~ s/^s+//; $s =~ s/s+z//; $s }

Or in Perl 5.14+,

sub trim(_) { $_[0] =~ s/^s+//r =~ s/s+z//r }

Then use:

my $Final = join '|', map trim, @$row;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...