我目前正在使用perl HTML :: Strip从我的HTML文件中提取文本,但是我遇到了一个HTML特定空格的小问题,即“”。出于某种原因,HTML :: Strip-> parse()似乎在这个实例中不起作用。我知道我以后可以运行replace命令。但我正在检查是否有另一种方法可以通过调整new()构造函数来实现这一目的?提前致谢
Perl代码:
my $hs = HTML::Strip->new();
my $line = join('',@htmlSource);
my $clean_text = $hs->parse( $line );
push @processedLines, grep { /\S/ } split (/\n/,$clean_text);
foreach my $f ( @processedLines ) {
print "$f\n";
}
示例输出:
CBD_UnitTest
MtrTempEst
MtrTempEst_Init1 (C1-Coverage: 100.00 %, 1 out of 1 Testcases passed)
LeadLagFilt (C1-Coverage: 100.00 %, 1 out of 1 Testcases failed)
AssMechFiltInit (C1-Coverage: 100.00 %, 1 out of 1 Testcases passed)
示例数据集:
<table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
<table width="100%" cellspacing="0" cellpadding="1" bgcolor="white" border="0">
<tr bgcolor="#dcdcdc">
<td width="1%" bgcolor="white">
<img border="0" src="pictures/batch_module_notok.jpg"/>
</td>
<td colspan="3" width="1%">
<font face="tahoma" size="-2" color="black">
CBD_UnitTest
</font>
</td>
<td width="1%">
</td>
<td width="1%">
</td>
<td width="1%">
<img border="0" src="pictures/batch_check_notok.gif"/>
</td>
</tr>
<tr bgcolor="white">
<td width="1%" bgcolor="white">
</td>
<td width="1%" bgcolor="white">
<img border="0" src="pictures/batch_module_notok.jpg"/>
</td>
<td colspan="2">
<font face="tahoma" size="-2" color="black">
MtrTempEst
</font>
</td>
<td width="1%">
</td>
<td width="1%">
</td>
<td width="1%">
<img border="0" src="pictures/batch_check_notok.gif"/>
</td>
</tr>
<tr bgcolor="#dcdcdc">
<td width="1%" bgcolor="white">
</td>
<td width="1%" bgcolor="white">
</td>
<td width="1%" bgcolor="white">
<img border="0" src="pictures/batch_ok.jpg"/>
</td>
<td>
<a href="#CBD_UnitTest:MtrTempEst:ts_MtrTempEst_Init1"><font face="tahoma" size="-2" color="black">
MtrTempEst_Init1 (C1-Coverage: 100.00 %, 1 out of 1 Testcases passed)
</font></a>
</td>
<td width="1%">
</td>
<td width="1%">
</td>
<td width="1%">
<img border="0" src="pictures/batch_check_ok.gif"/>
</td>
</tr>
<tr bgcolor="#FF0000">
<td width="1%" bgcolor="white">
</td>
<td width="1%" bgcolor="white">
</td>
<td width="1%" bgcolor="white">
<img border="0" src="pictures/batch_notok.jpg"/>
</td>
<td>
<a href="#CBD_UnitTest:MtrTempEst:ts_LeadLagFilt"><font face="tahoma" size="-2" color="white">
<b>LeadLagFilt (C1-Coverage: 100.00 %, 1 out of 1 Testcases failed)</b>
</font></a>
</td>
<td width="1%">
<a name="LeadLagFilt_0"></a>
</td>
<td width="1%">
</td>
<td width="1%">
<img border="0" src="pictures/batch_check_notok.gif"/>
</td>
</tr>
<tr bgcolor="#dcdcdc">
<td width="1%" bgcolor="white">
</td>
<td width="1%" bgcolor="white">
</td>
<td width="1%" bgcolor="white">
<img border="0" src="pictures/batch_ok.jpg"/>
</td>
<td>
<a href="#CBD_UnitTest:MtrTempEst:ts_AssMechFiltInit"><font face="tahoma" size="-2" color="black">
AssMechFiltInit (C1-Coverage: 100.00 %, 1 out of 1 Testcases passed)
</font></a>
</td>
<td width="1%">
</td>
<td width="1%">
</td>
<td width="1%">
<img border="0" src="pictures/batch_check_ok.gif"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
答案 0 :(得分:1)
你安装了HTML::Entities? docs for HTML::Strip州:
“如果安装了HTML :: Entities,HTML :: Strip将只尝试解码HTML实体。”
答案 1 :(得分:1)
从上面的HTML :: Entities链接中找出答案。 Thx @edibleEnergy
use HTML::Strip;
use HTML::Entities;
my $hs = HTML::Strip->new();
my $line = join('',@htmlSource);
_decode_entities($line, { nbsp => "" }, 1);
my $clean_text = $hs->parse( $line );
push @processedLines, grep { /\S/ } split (/\n/,$clean_text);
foreach my $f ( @processedLines ) {
print "$f\n";
}
我知道我们可以在这里使用简单的替换(即s / \ // g)但是上面的例子可以使用或不使用“;”在末尾。请查看@foodEnergy答案中提供的链接。