div 内のタグがわかっている場合、Perl の HTML モジュールを使用して div の内容を見つけるにはどうすればよいですか?
-
18-09-2019 - |
質問
正規表現で HTML を解析する方法を尋ねて少しバッシングされて以来 (当然のことですが)、私はずっと勉強してきました。 HTML::ツリービルダー, HTML::パーサー, HTML::TokeParser, 、 そして HTML::要素 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";
}
}
解決
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' ] ];
他のヒント
(さらに別のモジュール!) を使用することもできます。 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
使用するコード 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.
}
変更する必要があります get_attr("a1")
に get_attr("id")
ここ。の get_attr (x)
という名前の属性を探しています x
, ただし、属性の名前ではなく、属性の値を指定しています。
ちなみに、 <dt>
タグはありません <div>
, のアイテムタグです。 <dl>
(定義リスト)。
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!;
}