如何使用Web :: Query从HTML文档中提取一些文本

时间:2015-06-17 20:29:27

标签: perl

我正在尝试使用Web :: Query在以下示例中提取主题(在h3标记之间)。查找'h3'返回作者文本,但我希望在主题类中使用h3。我试过.subject.div.h3但它返回undef。

#!/usr/bin/perl

use strict;
use warnings;
use Web::Query; # libweb-query-perl
use Data::Dumper;

my $testhtml ='
<html><head></head>
<body>
   <div class="author"
      <div><h3>Neil Watson</h3></div>
   </div>
   <div class="subject">
      <div><h3>@if version_after macro is illogical</h3></div>
   </div>
</body>
</html>
';

my $parts = Web::Query->new_from_html( $testhtml );
my $subject = $parts->find( 'div.subject.div.h3' )->text;

print "subjectfinal ".Dumper( $subject );

1 个答案:

答案 0 :(得分:2)

点选择器表示类选择,这不是你想要的第二个div和h3。对于这些你想要后代。正确的语法是;

my $subject = $parts->find( 'div.subject > div > h3' )->text;

# Which outputs
# subjectfinal $VAR1 = '@if version_after macro is illogical';

有关CSS选择器的更多信息,这是基于Web :: Query的基础,请查看http://www.w3schools.com/cssref/css_selectors.asp