简体   繁体   中英

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 class="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>

我将使用HTML::Tree解析HTML,然后找到具有所需属性的节点,对其进行更改,然后输出将具有所需更改的新树。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM