为什么 HTML::Element 中的 look_down 方法找不到 <section> 元素?

Why does look_down method in HTML::Element fail to find <section> elements?

下面的代码显示 TreeBuilder 方法 look_down 找不到 "section" 元素。为什么?

use strict;
use warnings;
use HTML::TreeBuilder;

my $html =<<'END_HTML';
<html>
<head><title></title></head>
<body>
<div attrname="div">
<section attrname="section">
</section>
</div>
</body>
</html>
END_HTML

my $tree = HTML::TreeBuilder->new_from_content($html);

my @divs = $tree->look_down('attrname', 'div');
print "number of div elements found = ", scalar(@divs), "\n";

my @sections = $tree->look_down('attrname', 'section');
print "number of section elements found = ", scalar(@sections), "\n";

$tree->delete();

输出: 找到的 div 个元素数 = 1 找到的部分元素数 = 0

my @divs = $tree->look_down('attrname', 'div');
print "number of div elements found = ", scalar(@divs), "\n";

这找到了一个元素,因为它与 属性 attrname 匹配,值 div 恰好是在 <div> 标签上。

my @sections = $tree->look_down('attrname', 'section');
print "number of section elements found = ", scalar(@sections), "\n";

这与任何内容都不匹配,因为没有标签具有名为 attrname 且值为 section.

的属性

他们应该是

my @divs = $tree->look_down(_tag => 'div');
...
my @sections = $tree->look_down(_tag => 'section');

这在 HTML::Element#lookdown 文档中都有些含糊不清的解释。没有明确解释 "criteria" 是什么,你必须阅读整个页面才能找到 pseudo-attribute _tag 来引用标签名称......但仔细阅读整个页面可能会在漫长的 运行 :-)

中为您节省数小时的挫败感

这对我有用:

my $tree = HTML::TreeBuilder->new;
$tree->ignore_unknown(0);  # <-- Include unknown elements in tree
$tree->parse($html);
my @divs = $tree->look_down('attrname', 'div');
my @sections = $tree->look_down('attrname', 'section');
print "number of div elements found = ", scalar(@divs), "\n";
print "number of section elements found = ", scalar(@sections), "\n";

输出:

number of div elements found = 1
number of section elements found = 1