Perl HTML :: Treebuilder :: Xpath无法找到子标签

时间:2015-09-17 12:16:03

标签: perl xpath

HTML结构是,

<div class="profile-content"><section class="content-section"><h3 class="subheader">Welcome to the party</h3>

我想打印

  

欢迎参加派对

代码如下,

my $profile= $tree->findvalue('//div[@class="profile-content"]/section[@class="content-section"]/h3[@class="subheader"]');

但它没有打印任何东西。

请帮帮我。

此致

1 个答案:

答案 0 :(得分:2)

<section>是HTML5标记,HTML :: TreeBuilder无法识别。默认情况下,它不存在于HTML的内部表示中。

ignore_unknown选项设置为0(false)可以为您提供所需的内容。

看到区别:

> perl -MHTML::TreeBuilder -E'my $t=HTML::TreeBuilder->new; $t->parse( q{<div class="profile-content"><section class="content-section"><h3 class="subheader">Welcome to the party</h3>}); say $t->as_HTML'
<html><head></head><body><div class="profile-content"><h3 class="subheader">Welcome to the party</h3></div></body></html>

> perl -MHTML::TreeBuilder -E'my $t=HTML::TreeBuilder->new; $t->ignore_unknown( 0); $t->parse( q{<div class="profile-content"><section class="content-section"><h3 class="subheader">Welcome to the party</h3>}); print $t->as_HTML'
<html><head></head><body><div class="profile-content"><section class="content-section"><h3 class="subheader">Welcome to the party</h3></section></div></body></html>