Cha*_*hak 0 regex perl html-parsing
大约有100个文件,我需要浏览每个文件并删除之间的所有数据<style>和</style>+删除这些标签.
例如
<html>
<head> <title> Example </title> </head>
<style>
p{color: red;
background-color: #FFFF;
}
div {......
...
}
</style>
<body>
<p> hi I'm a paragraph. </p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
应该成为
<html>
<head> <title> Example </title> </head>
<body>
<p> hi I'm a paragraph. </p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
此外,在某些文件中,样式模式也是如此
<style type="text/css"> blah </style>
Run Code Online (Sandbox Code Playgroud)
要么
<link rel="stylesheet" type="text/css" href="$url_path/gridsorting.css">
Run Code Online (Sandbox Code Playgroud)
我需要删除所有3种模式.我如何在Perl中执行此操作?
use strict;
use warnings;
use XML::LibXML qw( );
my $qfn = 'a.html';
my $doc = XML::LibXML->load_html( location => $qfn );
my $root = $doc->documentElement();
for my $style_node ($root->findnodes('//style')) {
$style_node->parentNode()->removeChild($style_node);
}
{
open(my $fh, '>', $qfn)
or die;
print($fh $doc->toStringHTML());
}
Run Code Online (Sandbox Code Playgroud)
它正确处理:
在此更新中,其他解决方案仅处理其中的2个或3个.
| 归档时间: |
|
| 查看次数: |
1505 次 |
| 最近记录: |