gpt4 book ai didi

perl - 如何在html树中向上移动节点并提取链接?

转载 作者:行者123 更新时间:2023-12-04 12:10:58 25 4
gpt4 key购买 nike

我知道我的问题标题不是那么具有描述性,但让我在这里解释一下。

我正在尝试解析给定的 html document使用 HTML::TreeBuilder。现在在这个 html 文档值 5,1,ABC,DEF将根据用户提供的值进行验证,如果验证成功,我必须提取 href关联。

所以,我的代码是:

my @tag = $tree->look_down( _tag => 'tr', class => qr{\bepeven\scompleted\b} );
for (@tag) {

query_element($_);
}

sub query_element {

my @td_tag = $_[0]->look_down( _tag => 'td' );

my $num1 = shift @td_tag; #Get the first td tag
my $num2 = shift @td_tag; # Get the second td tag


#Making sure first/second td tag has numeric value
$num1 = $1 if $num1->as_text =~ m!(\d+)! or die "no match found";
$num2 = $1 if $num2->as_text =~ m!(\d+)! or die "no match found";


#Validating that above value's match the user provided value 5 and 1.
if ( $num1 eq '5' && $num2 eq '1' ) {
say "hurray..!!";

#Iterating over rest of the td tag to make sure we get the right link from it.
for (@td_tag) {

#Check if contains ABC and than procede to fetch the download href link.
if ($_->look_down(_tag => 'td', class => qr{[c]}, sub {
$_[0]->as_text eq 'ABC';} )
)
{
my $text = $_->as_text;
say "Current node text is: ", $text; #outputs ABC

#Now from here how do I get the link I want to extract.
}
}
}
}

现在,我的方法是首先从 td tags 中提取值如果成功,则将其与用户指定的值进行匹配,而不是查找另一个用户指定的值 ABC or DEF就我而言,它是 ABC如果匹配 不仅仅是提取链接。

现在,标记包含 ABC or DEF没有固定位置,但它们将在包含 5 and 1 的标签下方值(value)。所以,我用了 $_[0]->as_text eq 'ABC';检查标签是否包含 ABC现在在我的树中,我目前在 text node ABC 从这里我如何提取链接 href i,e 如何向上移动对象树并提取值。

PS:我会在这里尝试 xpath,但 html 元素的位置不是那么明确和结构化。

编辑:

所以,我尝试了 $_->tag()并返回 td但是如果我在 td 标签上而不是为什么下面的代码不起作用:
my $link_obj = $_->look_down(_tag => 'a') # It should look for `a` tag.
say $link_obj->as_text;

但它给出了以下错误:
Can't call method "as_text" on an undefined value.

最佳答案

我希望以下(使用我自己的 Marpa::R2::HTML)对您有所帮助。注意
HTML::TreeBuilder 答案只能找到一个答案。下面的代码找到
二,我认为这是意图。

#!perl

use Marpa::R2::HTML qw(html);

use 5.010;
use strict;
use warnings;

my $answer = html(
( \join q{}, <DATA> ),
{ td => sub { return Marpa::R2::HTML::contents() },
a => sub {
my $href = Marpa::R2::HTML::attributes()->{href};
return undef if not defined $href;
return [ link => $href ];
},
'td.c' => sub {
my @values = @{ Marpa::R2::HTML::values() };
if ( ref $values[0] eq 'ARRAY' ) { return $values[0] }
return [ test => 'OK' ] if Marpa::R2::HTML::contents eq 'ABC';
return [ test => 'OK' ] if Marpa::R2::HTML::contents eq 'DEF';
return [ test => '' ];
},
tr => sub {
my @cells = @{ Marpa::R2::HTML::values() };
return undef if shift @cells != 5;
return undef if shift @cells != 1;
my $ok = 0;
my $link;
for my $cell (@cells) {
my ( $type, $value ) = @{$cell};
$ok = 1 if $type eq 'test' and $value eq 'OK';
$link = $value if $type eq 'link';
}
return $link if $ok;
return undef;
},
':TOP' => sub { return Marpa::R2::HTML::values(); }
}
);

die "No parse" if not defined $answer;
say join "\n", @{$answer};

__DATA__
<table>
<tbody>

<tr class="epeven completed">
<td>5</td>
<td>1</td>
<td class="c">ABC</td>
<td class="c">satus</td>
<td class="c"><a href="/path/link">Download</a></td>
</tr>
<tr class="epeven completed">
<td>5</td>
<td>1</td>
<td class="c">status</td>
<td class="c">DEF</td>
<td class="c"><a href="/path2/link">Download</a></td>
</tr>


</table>

关于perl - 如何在html树中向上移动节点并提取链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12401684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com