aks
aks

Reputation: 1359

How do I get continuous tags with HTML::TokeParser

I have html that looks like this,

<div>
   <span>Text 
       <a href="example.html">ABC</a>
       <a href="example.html">DEF</a>
       <a href="example.html">HIJ</a>
       <a href="example.html">KLM</a>
   </span>
   <p class="Time">
   09/28/10 - 03:46 PM EDT</p>
</div>

I need to loop through the <a> tags between the <span> tags till I get the text from all of them. This keeps changing from time so I do not know howmany <a> tags would exist. I cant do a,

while ( $tag = $stream->get_tag('a') ) 

because it browses through the <a> tags in the entire file. How can I make it stop when continuous <a> tags end?

Upvotes: 0

Views: 238

Answers (1)

cjm
cjm

Reputation: 62109

while ( $tag = $stream->get_tag(qw( a /span )) ) {
  last if $tag->[0] eq '/span';
  ...
}

Another approach would be to get any tag, and stop if it isn't <a>:

while ( $tag = $stream->get_tag ) {
  last unless $tag->[0] eq 'a';
  ...
}

Upvotes: 3

Related Questions