Mojo::DOM extract paragraph after specific previous paragraph

69 views Asked by At

Just using this Mojo::DOM for the first time and having trouble to extract information based on a previous tag. Looking for a way to grab 'The description'.

#!/usr/bin/perl
require v5.10;
use feature qw(say);
use Mojo::DOM;

my $html = q(<p><strong>Description</strong></p><p>The description</p> <p><strong>Usage</strong></p><p>How to use this tool</p>);

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

say $dom->find('p strong')->map('text')->join("\n"); # Description

1

There are 1 answers

1
elcaro On

To get the first one, you can do this

say $dom->at('p strong')->parent->next->text;

Or to get all of them

say $dom->find('p strong')->map(sub { $_->parent->next->text })->join("\n");