
[英]In HTML parsing, will the <script> tag be inserted into the DOM tree?
[英]HTML tag parsing script
我写了一个HTML标记解析脚本,我认为该脚本可以运行,但是出现文件未找到错误。 也许我有一个高级的时刻,但是我被困住了。 我要解析的所有* .html文件都位于一个名为Test
的目录中,我正在从一个名为temp
的文件夹中执行perl脚本,该文件夹中包含目录Test。 确切的错误是: 打开Test / 1.html时出错:没有这样的文件或目录。 这是代码:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use HTTP::Headers;
use HTML::HeadParser;
use Text::CSV;
my $csv1 = Text::CSV->new ( { binary => 1 } ) or die Text::CSV->error_diag();
$csv1->eol ("\n");
my $dfile = 'all_tags.csv';
open my $fh1, ">:encoding(utf8)", "$dfile" or die "Error opening $dfile: $!";
my $dir = 'Test';
find (\&HTML_Files, $dir);
print "directory is";
print $dir;
close $fh1 or die "Error closing $dfile: $!";
exit;
sub HTML_Files {
Parse_HTML_Header($File::Find::name) if /\.html?$/;
}
sub Parse_HTML_Header {
my $ifile = shift;
open(my $fh0, '<', $ifile) or die "Error opening $ifile: $!\n";
my $text = '';
{
$/ = undef;
$text = <$fh0>;
}
close $fh0;
my $h = HTTP::Headers->new;
my $p = HTML::HeadParser->new($h);
$p->parse($text);
for ($h->header_field_names) {
my @values = split ',', $h->header($_);
if (/keywords/i) {
$csv1->print ($fh1, \@values);
} elsif (/description/i) {
$csv1->print ($fh1, \@values);
} elsif (/title/i) {
$csv1->print ($fh1, \@values);
}
}
}
这是因为File::Find
在运行时正在执行chdir
。 您应该传递$_
而不是$File::Find::name
。 或设置no_chdir
:
no_chdir
递归时,不要对每个目录使用chdir()。 当然,wanted()函数将需要意识到这一点。 在这种情况下,$ _将与$ File :: Find :: name相同。
因为您正在指定相对路径,所以$File::Find::name
也是相对路径。 您也可以通过指定完整的find
路径来避免这种情况。 (例如/full/path/to/dir
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.