in reply to Re^2: Mojo::DOM find tag after another tag
in thread Mojo::DOM find tag after another tag
Hi luxs,
Since I'm guessing the <img> tags can be nested arbitrarily deep, the CSS selector "img, h1" was going to be my next suggestion.
There are many other ways to write the logic, here's one:
use warnings; use strict; use Mojo::DOM; my $data = <<'ENDDATA'; yada...yada...yada.. <img src="111"> yada...yada...yada.. <img src="222"> yada...yada...yada.. <h1>Some title</h1> yada...yada...yada.. <a href="444444"> <img src="333"> yada...yada...yada.. <img src="444"> ENDDATA my $dom = Mojo::DOM->new($data); my (@imgs,$seen_h1); for my $tag ( $dom->find('h1, img')->each ) { if ($seen_h1 && $tag->tag eq 'img' && length $tag->attr('src')) { push @imgs, $tag } elsif ($tag->tag eq 'h1') { $seen_h1 = 'true' } } print $_->attr('src'),"\n" for @imgs; __END__ 333 444
You don't even need the intermediate @imgs array and can print directly from the first loop if you like.
Hope this helps,
-- Hauke D
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Mojo::DOM find tag after another tag
by luxs (Beadle) on May 29, 2016 at 20:29 UTC |