我正在使用 HTML::TreeBuilder 来处理 HTML 文件。在这些文件中,我可以有定义列表,其中有术语“数据库”和定义“数据库名称”。模拟的 html 如下所示:
#!/usr/bin/perl -w
use strict;
use warnings;
use HTML::TreeBuilder 5 -weak;
use feature qw( say );
my $exampleContent = '<dl>
<dt data-auto="citation_field_label">
<span class="medium-bold">Language:</span>
</dt>
<dd data-auto="citation_field_value">
<span class="medium-normal">English</span>
</dd>
<dt data-auto="citation_field_label">
<span class="medium-bold">Database:</span>
</dt>
<dd data-auto="citation_field_value">
<span class="medium-normal">Data Archive</span>
</dd>
</dl>';
my $root = HTML::TreeBuilder->new_from_content($exampleContent);
my $dlist = $root->look_down("_tag" => "dl");
foreach my $e ($dlist->look_down("_tag" => 'dt', "data-auto" => "citation_field_label")) {
if ($e->as_text =~ m/Datab.*/) {
say $e->as_text; # I have found "Database:" 'dt' field
# now I need to go to the next field 'dd' and return the value of that
}
}
我需要确定文件来自哪个数据库并返回值。
say $dlist->right()->as_text;
当我在其中识别出“数据库:”时,我希望能够说出类似<dt>
的话,但我不知道该怎么做。您的想法将不胜感激。