这段代码效果很好,但有些行不遵循表方案。有时有这样的一行
<td colspan="7">
<div class="note">lots of notes here</div>
</td>
这会导致CSV中断,有没有办法使用以下代码忽略这些类型的行?
my $te = 'HTML::TableExtract'
->new(headers => ['Data1', 'Data 2', 'Data 3', 'Data 4',
'Data 4', 'Data 5', 'Data 6']);
my $csv = 'Text::CSV'->new({ binary => 1,
eol => "n",
always_quote => 1,
});
while (@ARGV) {
my $file = shift;
open my $IN, '<', $file or die $!;
my $html = do { local $/; <$IN> };
$te->parse($html);
}
for my $table ($te->tables) {
$csv->print(*STDOUT{IO}, $_) for $table->rows;
}
下面是一个较大的 HTML 文件示例:
<table class="datalogs" cellspacing="5px">
<tr><th>Data1</th><th>Data 2</th><th>Data 3</th><th>Data 4</th><th>Data 4< /th>< th>Data 5</th><th>Data 6</th></tr>
<tr class="odd"><td valign="top"><h4>123<br/></h4></td><td valign="top">AAA</td><td valign="top"><b>url here</b></td><td valign="top">Yes</td><td valign="top">None</td><td valign="top"></td><td valign="top"></td></tr><tr class="even">...</td></tr>
<td colspan="7">
<div class="note">lots of notes here</div>
</td>
</table>
下面是当前的示例输出:
"Other","JPEG","http://URL/jpg/image.jpg","No","None",,
"Other","JPEG","http://URL/","Yes","None",,
"Other","PNG","http://URL:80/","Yes","None",,
"Othe","GIF","http://URL/GetData?y=1","No","None",,
感谢您的帮助
I 文档:
具有行跨度或列跨度属性的表将有一些单元格 包含未定义。
您需要检查所有 7 个单元格是否都具有 undef 值,而不是忽略行。
my $te = 'HTML::TableExtract'
->new(headers => ['Data1', 'Data 2', 'Data 3', 'Data 4',
'Data 4', 'Data 5', 'Data 6']);
my $csv = 'Text::CSV'->new({ binary => 1,
eol => "n",
always_quote => 1,
});
while (@ARGV) {
my $file = shift;
open my $IN, '<', $file or die $!;
my $html = do { local $/; <$IN> };
$te->parse($html);
}
for my $table ($te->tables) {
for my $row ($table->rows){
my @val = grep { defined $_ } @{$row} ;
next if scalar( @val) == 1;
$csv->print(*STDOUT{IO}, $row);
}
}