自从我问到如何使用正则表达式解析html并得到了一些抨击(理所当然)之后,我一直在研究HTML::TreeBuilder,HTML::Parser,HTML::TokeParser和{{3 Perl模块。
我有这样的HTML:
<div id="listSubtitlesFilm">
<dt id="a1">
<a href="/45/subtitles-67624.aspx">
.45 (2006)
</a>
</dt>
</div>
我想解析/45/subtitles-67624.asp
,但更重要的是我想知道如何解析div的内容。
我在前一个问题上得到了这个例子:
while ( my $anchor = $parser->get_tag('a') ) {
if ( my $href = $anchor->get_attr('href') ) {
#http://subscene.com/english/Sit-Down-Shut-Up-First-Season/subtitles-272112.aspx
push @dnldLinks, $1 if $href =~ m!/subtitle-(\d{2,8})\.aspx!;
}
这完全适用于此,但当我尝试编辑它并在``div`上使用它时它不起作用。这是我试过的代码:
我尝试使用此代码:
while (my $anchor = $p->get_tag("dt")) {
if($stuff = $anchor->get_attr('a1')) {
print $stuff."\n";
}
}
答案 0 :(得分:5)
您可以使用(还有另一个模块!)HTML::TreeBuilder::XPath,根据其名称,它将允许您在HTML :: TreeBuilder对象上使用XPath。
#!/usr/bin/perl
use strict;
use warnings;
use HTML::TreeBuilder::XPath;
my $root = HTML::TreeBuilder::XPath->new_from_file( "my.html");
# print $root->as_HTML; # useful to see how HTML::TreeBuilder
# understands your HTML. For example it will wrap the implied
# dl element around dt, which you need to take into account
# when writing the XPath query below
my $id= "a1";
# you need the .//dt because of the extra dl
my @divs= $root->findnodes( qq{//div[.//dt[\@id="$id"]]});
print $divs[0]->as_HTML; # or as_text
答案 1 :(得分:5)
根据HTML:
解决您的具体问题<div id="listSubtitlesFilm">
<dt id="a1">
<a href="/45/subtitles-67624.aspx">
.45 (2006)
</a>
</dt>
</div>
我假设您对锚文本感兴趣,即".45 (2006)"
,在这种情况下,但仅当锚点出现在div
且ID为listSubtitlesFilm
时才会生效。
#!/usr/bin/perl
use strict;
use warnings;
use HTML::TokeParser::Simple;
my $parser = HTML::TokeParser::Simple->new(handle => \*DATA);
my @dnldLinks;
while ( my $div = $parser->get_tag('div') ) {
my $id = $div->get_attr('id');
next unless defined($id) and $id eq 'listSubtitlesFilm';
my $anchor = $parser->get_tag('a');
my $href = $anchor->get_attr('href');
next unless defined($href)
and $href =~ m!/subtitles-(\d{2,8})\.aspx\z!;
push @dnldLinks, [$parser->get_trimmed_text('/a'), $1];
}
use Data::Dumper;
print Dumper \@dnldLinks;
__DATA__
<div id="listSubtitlesFilm">
<dt id="a1">
<a href="/45/subtitles-67624.aspx">
.45 (2006)
</a>
</dt>
</div>
输出:
$VAR1 = [ [ '.45 (2006)', '67624' ] ];
答案 2 :(得分:4)
使用HTML::TreeBuilder
的代码:
use HTML::TreeBuilder;
my $tree = HTML::TreeBuilder->new_from_content($html);
for my $link ($tree->look_down(
_tag => 'a',
href => qr{/subtitle-\d{2,8}\.aspx})
) {
my $linkid = $link->attr('href') =~ m!/subtitle-\d{2,8}\.aspx!;
# Scalar context gets the first, and the first is the nearest parent
my $parent_div = $link->look_up(_tag => 'div');
# Now the interesting bit of the link is in $linkid, the parent div ID
# is $parent_div->id or $parent_div->attr_id, and its text is e.g.
# $parent_div->as_trimmed_text or you can do other stuff with its content.
}
答案 3 :(得分:1)
get_attr('a1')
可能应该阅读get_attr('id')
并打印“a1”
我认为获取文本内容看起来像是:
while ( my $anchor = $parser->get_tag('div') ) {
my $content = $parser-get_text('/div');
}
或者如果你的意思是链接的文本内容,那就是:
while ( my $anchor = $parser->get_tag('a') ) {
if ( my $href = $anchor->get_attr('href') ) {
my $content = $parser->get_text('/a');
#http://subscene.com/english/Sit-Down-Shut-Up-First-Season/subtitle-272112.aspx
push @dnldLinks, $1 if $href =~ m!/subtitle-(\d{2,8})\.aspx!;
}
答案 4 :(得分:1)
您需要在此处将get_attr("a1")
更改为get_attr("id")
。 get_attr (x)
正在查找名为x
的属性,但您要为其提供属性的值,而不是其名称。
顺便提一下,<dt>
标记不是<div>
,而是<dl>
(定义列表)的商品标记。