开发者

How can I extract and format HTML found in a div tag, using Perl?

Note:

  • Using HTML::TreeBuilder or other suitable method

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
wh开发者_运维百科atever
test 


$node->find_by_attribute(attribute, value) and $node->as_text() 

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


my @elms = $tree->look_down(
  _tag => "div",
  sub { $_[0]->as_text =~ /whatever/ },
);

for my $elm (@elms) {
  print $elm->as_trimmed_text;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜