Joe
Joe

Reputation: 245

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

I have a page which contains this:

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

How can I get the anchor text, URL and title?

I want to have this output:

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

I have tried to use WWW::Mechanize as explained also here, but I do not know how to get the title in this way. Do you have any ideas?

Upvotes: 0

Views: 787

Answers (2)

vanHoesel
vanHoesel

Reputation: 954

The simple version, based on your question

  • a page that looks like yours (so no obscure html that can mess up)
  • te desired output

This might be what you are looking for:

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"
}

Happy coding, TIMTOWTDI

Upvotes: 2

Kirs Kringle
Kirs Kringle

Reputation: 929

Using the documentation provided in your question. I created something that solves your problem I believe. Obviously using https://www.perlmonks.org has some outliers as some of the URLs are not full URLs, but with some simple checking and skipping if it is not what you want I think you'd get what you want.

Example Output:

_____________________________________________________________________________________________________________________________
| 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,  |

Likely you were stumped at the hashref. You just needed to make a for loop to go through those to get the attribute tags and their values.

Code:

#!/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};
    }
}

Upvotes: 0

Related Questions