开发者

How can I replace some HTML tags based on their class in Perl?

I need replace some tags in HTML using Perl:

I have this:

<span cla开发者_StackOverflow中文版ss="a">text</span><span class="a">text</span><span id="b">text</span>

I need this, where the span tags with class=a are changed to b tags instead:

<b>text</b><b>text</b><span id="b">text</span>

I tried using HTML::Manipulator but did not succeed.


Here's how to use HTML::TreeBuilder:

use strict;
use warnings;
use HTML::TreeBuilder;

my $html_string = '<span class="a">text</span><span class="a">text</span><span id="b">text</span>';    

my $root = HTML::TreeBuilder->new_from_content($html_string);
$root->elementify;  # Make $root into an HTML::Element object;


for my $e ( $root->look_down( _tag => 'span', class => 'a' ) ) {
    $e->tag( 'b' );
    $e->attr( class => undef );
} 

print $root->as_HTML;


An example using HTML::Parser:

#! /usr/bin/perl

use warnings;
use strict;
use HTML::Parser;
my $p = HTML::Parser->new( api_version => 3,
  start_h => [\&start, "tagname, attr, text, skipped_text"],
  end_h   => [\&end,   "tagname,       text, skipped_text"],
);
$p->parse_file(\*DATA);

my @switch_span_end;
sub start {
  my($tag,$attr,$text,$skipped) = @_;
  print $skipped;
  unless ($tag eq 'span' && ($attr->{class}||"") eq "a") {
    print $text;
    return;
  }
  push @switch_span_end => 1;
  print "<b>";
}

sub end {
  my($tag,$text,$skipped) = @_;
  print $skipped;
  if (@switch_span_end && $tag eq "span") {
    print "</b>";
    pop @switch_span_end;
  }
  else {
    print $text;
  }
}
__DATA__
<span class="a">text</span><span class="a">text</span><span id="b">text</span>

Output:

<b>text</b><b>text</b><span id="b">text</span>


I would use HTML::Tree to parse the HTML, then find the nodes that have the properties you want, change them, then output the new tree, which will have the changes you desire.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜