With HTML::TokeParser::Simple

(assumes all links within class="full")

#!/usr/bin/perl use strict; use warnings; use HTML::TokeParser::Simple; my $html = do {local $/; <DATA>}; my $p = HTML::TokeParser::Simple->new(\$html) or die "can't parse: $!"; my ($in_full, @href); while (my $t = $p->get_token){ next if $t->is_start_tag('div') and $t->get_attr('class') and $t->get_attr('class') eq 'content'; $in_full++, next if $t->is_start_tag('div') and $t->get_attr('class') eq 'full'; $in_full = 0, next if $t->is_start_tag('div') and $t->get_attr('class') ne 'full'; next unless $in_full; push @href, $t->get_attr('href') if $t->is_start_tag('a'); } print "$_\n" for @href; __DATA__ <!-- some other divs and so here --> <div class="full"> <div class="content"> <ul class="topics"> <-- I want extract these links div class "full" only --> <li><a href="foobar">foobar</a></li> <li><a href="foobr2">fobar2</a></li> <li><a href="fobar3">foobr3</a></li> </ul> </div> </div> <div class="otherclass"> <div class="content"> <ul class="topics"> <-- I DO NOT WANT these links --> <li><a href="fbaor">fbaor</a></li> <li><a href="fabar2">fabar2</a></li> <li><a href="fbar3">fbar3</a></li> </ul> </div> </div> <!-- some other divs and so here --> <div class="full"> <div class="content"> <ul class="topics"> <-- I want extract these links div class "full" only --> <li><a href="foobar4">foobar4</a></li> <li><a href="foobr5">fobar5</a></li> <li><a href="fobar6">foobr6</a></li> </ul> </div> </div> <div class="otherclass"> <div class="content"> <ul class="topics"> <-- I DO NOT WANT these links --> <li><a href="fbaor">fbaor</a></li> <li><a href="fabar2">fabar2</a></li> <li><a href="fbar3">fbar3</a></li> </ul> </div> </div> <!-- some other divs and so here -->

In reply to Re: easy HTML::TokeParser help request by wfsp
in thread easy HTML::TokeParser help request by 2ge

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.