English 中文(简体)
正在删除 perl 中特定的 html 标记
原标题:Removing specific html tags in perl
  • 时间:2012-05-25 06:14:45
  •  标签:
  • perl

我在磁盘上存储了一个 html 文件( 文件是 < a href=> http:// home. howstuffworks. com/ home- improprovement/ building/ projects/ return- on- investment- from- remodel2.htm > rel= "nofollow" > this ) 。 我想删除图像的所有 html 标签 。 这是我迄今尝试过的 。

#!/usr/bin/perl -w
use HTML::TagFilter;
my $tf = new HTML::TagFilter;

open READ, "D:\Scripts\file.html" or die "Couldn t open file: $!"; 
$string1 = join("", <READ>); 
close READ;

my $self = HTML::TagFilter->new(deny => {img => { all }});
open (MYFILE,  >D:\Scripts\remove.html );
print MYFILE $tf->filter($string1);
close (MYFILE); 

只要我运行这个程序 它就印了

Odd number of elements in anonymous hash at remove everything else.pl line 9.
Parsing of undecoded UTF-8 will give garbage when decoding entities at C:/Perl64
/site/lib/HTML/TagFilter.pm line 499.

文件已存储, 但还没有删除图像标记( 第9行是我应用过滤器的地方) 。 我在这里做错了什么 。

最佳回答

首先,在程序启动时,你应该ways 严格使用 使用警告 ,特别是在请求帮助修复之前。

您创建了两个 < code> HTML:: TagFilter 对象: $tf 没有过滤器的 $tf 和删除 元素的 。 您使用 $tf 处理 HTML, 使您的数据保持不变 。

这个守则起作用了,我提到的更正和其他一些更正也起作用了。

use strict;
use warnings;

use HTML::TagFilter;

my $tf = HTML::TagFilter->new(deny => {img => {all => []}});

my $html = do {
  open my $fh,  D:Scriptsfile.html  or die "Couldn t open file: $!";
  local $/;
  <$fh>;
};

open my $out,  > ,  D:Scripts
emove.html  or die "Unable to open output file: $!";
print $out $tf->filter($html);
问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...