Будьте последовательны! ПРИСОЕДИНЕНИЮ к VARCHAR к NVARCHAR поражали большую производительность.
Чтобы ответить на ваш конкретный вопрос, учитывая HTML:
<div id="listSubtitlesFilm">
<dt id="a1">
<a href="/45/subtitles-67624.aspx">
.45 (2006)
</a>
</dt>
</div>
Я предполагаю, что вас интересует якорный текст, то есть ". 45 (2006)"
, в этом случае, но только если привязка встречается в div
с идентификатором 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
:
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.
}
Вы можете использовать (еще один модуль!) HTML :: TreeBuilder :: XPath , который, согласно его названию, позволит вам использовать XPath в HTML :: TreeBuilder объекты.
#!/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
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!;
}
Вам нужно изменить get_attr ("a1") [ С 1146322] на
get_attr ("id")
здесь. get_attr (x)
ищет атрибут с именем x
, но вы даете ему значение атрибута, а не его имя.
Между прочим, Тег
не является
(список определений).