使用Perl提取表内容 [英] Extract Table Contents using Perl
本文介绍了使用Perl提取表内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用HTML :: TableExtract从html文件中提取表内容.我的问题是我的html文件以以下方式构造:
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|
不是我想要的:
100|200|300|
400|500|600|
可以帮忙吗?这是我的perl代码
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);
推荐答案
sub trim(_) { my ($s) = @_; $s =~ s/^\s+//; $s =~ s/\s+\z//; $s }
或者在Perl 5.14+中,
Or in Perl 5.14+,
sub trim(_) { $_[0] =~ s/^\s+//r =~ s/\s+\z//r }
然后使用:
my $Final = join '|', map trim, @$row;
这篇关于使用Perl提取表内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文