tags:

views:

149

answers:

2

Note:

Question:

  • Using Perl with LWP, for the following HTML, how to search for the literal string whatever between the start tag and end tag div and then get all text between the aforementioned start and end tag, while adhering to formatting text tags

    <div id="foo" class="blah">
    <tt>
    test
    <br>test 
    <br>whatever
    <br>test
    </div>
    

To print to STDOUT:

test
test
whatever
test
+4  A: 
$node->find_by_attribute(attribute, value) and $node->as_text()

http://lwp.interglacial.com/ch09_03.htm

el.pescado
@el-pescado: thanks, but I'd like to search for text between a specified start and end tag first, before obtaining the text accordingly...
Aaron
@Aaron: I believe that El-pescado's answer tells you (implicitly) how to do what you want. `HTML::Element` gives you `find_by_attribute` and `find_by_tag` methods that should get you there.
Telemachus
+2  A: 
my @elms = $tree->look_down(
  _tag => "div",
  sub { $_[0]->as_text =~ /whatever/ },
);

for my $elm (@elms) {
  print $elm->as_trimmed_text;
}
hobbs
Thank you hobbs!
Aaron