Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Mojo::DOM find tag after another tag

by luxs (Beadle)
on May 29, 2016 at 19:37 UTC ( [id://1164457]=note: print w/replies, xml ) Need Help??


in reply to Re: Mojo::DOM find tag after another tag
in thread Mojo::DOM find tag after another tag

The only script I've manage to do is
my $tmpi = 0; $fimg = $dom ->find('img, h1') ->map( sub { if( $_->tag eq 'h1' ) { $tmpi++; } if( $tmpi > 0 && defined $_->attr->{'src'} ) { $_->attr->{'src'} } # without semicolumn!!!! } ); for( my $ii = 0; $ii <= $#$fimg; $ii++ ) { if( length( $fimg->[$ii] ) > 0 ) { print $fimg->[$ii]; } }
looks too complicated and not nice (but working).

Replies are listed 'Best First'.
Re^3: Mojo::DOM find tag after another tag
by haukex (Archbishop) on May 29, 2016 at 19:54 UTC

    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

      Yes, this way looks much more neat. Still struggle a bit to understand the logic of Mojo data and structure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1164457]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found