繁体   English   中英

如何在Perl中提取URL标记并从HTML链接文本?

[英]How can I extract URL tags and link text from HTML in Perl?

我有一个包含以下内容的页面:

<a href="http://www.trial.com" title="yellow">Trial</a>
<a href="http://www.trial1.com" title="red">Trial2</a>

如何获取锚文本,URL和标题?

我想要这个输出:

Trial, http://www.trial.com, yellow
Trial2, http://www.trial1.com, red

我已经尝试使用WWW :: Mechanize ,如此处所述 ,但我不知道如何以这种方式获得标题。 你有什么想法?

简单版本,根据您的问题

  • 看起来像您的页面(因此,不会混淆的html可能会搞乱)
  • 所需的输出

这可能是您要寻找的:

use strict;
use warnings;

use WWW::Mechanize;

my $mech = WWW::Mechanize->new;
$mech->get('file:page.html');

foreach my $link ($mech->links) {
    my $text  = $link->text;
    my $url   = $link->url;
    my $title = $link->attrs->{title};

    print "$text, $url, $title\n"
}

快乐编码,TIMTOWTDI

使用问题中提供的文档。 我创造了可以解决您所相信的问题的东西。 显然,使用https://www.perlmonks.org存在一些异常,因为某些URL不是完整的URL,但是通过一些简单的检查和跳过(如果不是所需的话),我想您会得到所需的。

示例输出

_____________________________________________________________________________________________________________________________
| Text                                            | URL                | Attributes
_____________________________________________________________________________________________________________________________
| Testing a metacpan dist with XS components      | ?node_id=1216149   | [name]post-head-id1216149[id]post-head-id1216149,  |
| Controlling the count in array                  | ?node_id=1216134   | [name]post-head-id1216134[id]post-head-id1216134,  |

可能您对hashref感到困惑。 您只需要创建一个for循环来遍历那些,即可获取属性标签及其值。

码:

#!/usr/bin/perl
# your code goes here
use strict;
use warnings;
use Data::Dumper;
use WWW::Mechanize ();
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;

my @urls = (
    q{https://www.perlmonks.org/}
);

my $mech = WWW::Mechanize->new();
$mech->get(@urls);

my @links = $mech->links();
print qq{______________________________________________________________________________________________________________\n};
printf(qq{| %-65s | %-75s | %-25s\n},q{Text},q{URL},q{Attributes});
print qq{______________________________________________________________________________________________________________\n};
foreach my $link (@links) {
    if ($link->text() && $link->url()) {
        my $a;
        foreach my $attr (keys %{$link->attrs()}) {
            next if $attr =~ m/href/i;

            #$link->attr()->{$attr} is the value of the key in this hashref. 
            $a .= qq{[$attr]} . $link->attrs()->{$attr};
        }
        my $info;
        if ($a) {
            $info = sprintf(qq{| %-65s | %-75s | %-25s, },$link->text(),$link->url(),$a);
        } else {
            $info = sprintf(qq{| %-65s | %-75s |},$link->text(),$link->url());
        }
        print $info . qq{ |\n};
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM