1

I'm new to Mojolicious, to find the title for a link within a p tag with class Module e.g.

<p class="Module"><a class="story" href="http://intranet/blah" >Link Text is here</a></p>

I use the following code:

my $dom = Mojo::DOM->new( $page );

for my $elm ( $dom->find('p.Module > a.story')->each ){
    print $elm->text ."\n";
}

Pretty crude but it's functional. What I can't figure out as yet (it could be too late at night for me) is how to return the href and the link text. Please put me out of my misery.

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
Dr.Avalanche
  • 1,944
  • 2
  • 28
  • 37

2 Answers2

4

You just need the attr method:

my $dom = Mojo::DOM->new( $page );

for my $elm ( $dom->find('p.Module > a.story')->each ){
    print $elm->text, ' ', $elm->attr('href'), "\n";
}

For a quick tutorial on Mojo::UserAgent and Mojo::DOM, check out Mojocast episode 5

Miller
  • 34,962
  • 4
  • 39
  • 60
2

Here's a mojo-y way of doing it by using Mojo::Collection's map:

use v5.10;

use Mojo::DOM;
use Data::Dumper;

my $page =<<'HTML';
<p class="Module"><a class="story" href="http://intranet/blah" >Link Text is here</a></p>
HTML

my $dom = Mojo::DOM->new( $page );

my @links = $dom
    ->find('p.Module > a.story')
    ->map( sub { [ $_->text, $_->attr( 'href' ) ] } );

say Dumper \@links;
brian d foy
  • 129,424
  • 31
  • 207
  • 592