我正在Perl中構建HTML解析器。我想知道HTML元素是否是沒有任何修改的元素。如何檢查HTML元素是否是一個結束節點?
這裏是HTML,我想解析:
<span class="bold1">A:</span> ELementA<br />
<span class="bold1">B:</span> <a href="mailto:admin" class="bold1">mailto:admin</a><br />
<span class="bold1">C </span> 01/12<br />
<span class="bold1">D:</span> ELementC<br />
<span class="bold1">E:</span> ElementD<br />
<span class="bold1">F:</span> ElementE<br />
如何檢查元素是結束元素。
我收到錯誤:
Can't call method "as_text" without a package or object reference at
任何想法可能是錯誤的?
這裏是Perl代碼片段,
my $mech = WWW::Mechanize->new(autocheck => 1);
eval
{
$mech->get($url);
};
if ([email protected])
{
print "Error connecting to URL $url \n";
exit(0);
}
my $root = HTML::TreeBuilder->new_from_content(decode_utf8($mech->content));
my @PageSections = $root->look_down(
sub {
return (
($_[0]->tag() eq 'span') and
($_[0]->attr('class') =~ m/bold1/i))
});
my $temp2;
my $temp3;
for my $ps (@PageSections)
{
# my $temp1= $ps->right()->as_text;
$temp2= $ps->as_text;
my $temp3=ref $ps->right();
#
print defined $temp3 ? "defined \n" : "not defined\n";
}
感謝
你使用的是[HTML-Tree](http://search.cpan.org/dist/HTML-Tree/)嗎? – cjm
不,我正在使用HTML :: Treebuilder – Kiran
[HTML :: TreeBuilder](http://search.cpan.org/perldoc?HTML::TreeBuilder)是[HTML-Tree](http:// search .cpan.org/dist/HTML-Tree /)分發(就像你已經看到你是否點擊鏈接一樣),所以你使用HTML-Tree。 (另外,Perl是區分大小寫的,所以它是HTML :: TreeBuilder,而不是HTML :: Treebuilder。) – cjm