如果需要从一个html网页中提取“XYZ 81.6(-0.1)”的信息,用perl怎么办?非常感谢。
<table border="0" width="100%">
<caption valign="top">
<p class="InfoContent"><b><br></b>
</caption>
<tr>
<td colspan="3"><p class="InfoContent"><b>ABC</b></td>
</tr>
<tr>
<td valign="top" height="61" width="31%">
<p class="InfoContent"><b><font color="#0000FF">XYZ 81.6 (-0.1) <br>22/06/2011</font></b></p>
</td>
</tr></table>
最佳答案
我会使用 HTML::TreeBuilder::XPath为此(是的,这是一个无耻的插件!):
#!/usr/bin/perl
use strict;
use warnings;
use HTML::TreeBuilder::XPath;
my $t= HTML::TreeBuilder::XPath->new_from_file( shift @ARGV);
my $text= $t->findvalue( '//p[@class="InfoContent"]/b/font[@color="#0000FF"]');
$text=~ s{\).*}{)};
print "found '$text'\n";
虽然它非常脆弱:据我所知,将 XPath 表达式缩小到您想要的范围的唯一方法是使用 font
标记。这在未来可能会改变,所以如果(当!)代码中断,那是您必须首先查看的地方。
关于perl - 如何使用perl从html网页中提取特定信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6454398/