我有一个。html文件充满了链接,我想提取没有http://的域(所以只是链接的主机名部分,例如blah.com)列出它们并删除重复。
这就是我到目前为止所提出的-我认为问题是我试图传递$tree数据的方式
#!/usr/local/bin/perl -w
use HTML::TreeBuilder 5 -weak; # Ensure weak references in use
use URI;
foreach my $file_name (@ARGV) {
my $tree = HTML::TreeBuilder->new; # empty tree
$tree->parse_file($file_name);
my $u1 = URI->new($tree);
print "host: ", $u1->host, "n";
print "Hey, here's a dump of the parse tree of $file_name:n";
# Now that we're done with it, we must destroy it.
# $tree = $tree->delete; # Not required with weak references
}
就我个人而言,我会使用Mojo::DOM,并使用URI模块来提取域:'
use Mojo::DOM;
use URI;
use List::AllUtils qw/uniq/;
my @domains = sort +uniq
map eval { URI->new( $_->{href} )->authority } // (),
Mojo::DOM->new( $html_code )->find("a[href]")->each;
(注:->authority
上的异常处理是因为一些URI在这里会崩溃;像mailto: s)
还有一个选项:
use strict;
use warnings;
use Regexp::Common qw/URI/;
use URI;
my %hosts;
while (<>) {
$hosts{ URI->new($1)->host }++ while /$RE{URI}{-keep}/g;
}
print "$_n" for keys %hosts;
命令行用法:perl script.pl htmlFile1 [htmlFile2 ...] [>outFile]
可以向脚本发送多个html文件。最后一个可选参数将输出指向一个文件。
部分输出使用cnn.com主页作为html来源:
www.huffingtonpost.com
a.visualrevenue.com
earlystart.blogs.cnn.com
reliablesources.blogs.cnn.com
insideman.blogs.cnn.com
cnnphotos.blogs.cnn.com
cnnpresents.blogs.cnn.com
i.cdn.turner.com
www.stylelist.com
js.revsci.net
z.cdn.turner.com
www.cnn.com
...
希望这对你有帮助!