1

我有一百万个网址的列表。我需要为每个 url 提取 TLD 并为每个 TLD 创建多个文件。例如,收集所有 .com 作为 tld 的 url,并将其转储到 1 个文件中,另一个文件用于 .edu tld 等等。在每个文件中,我必须按域的字母顺序对其进行排序,然后按子域等进行排序。

任何人都可以让我在 perl 中实现这一点吗?

4

1 回答 1

6
  1. 使用URI解析 URL,
  2. 使用它的host方法获取主机,
  3. 使用Domain::PublicSuffixget_root_domain解析主机名。
  4. 使用tldorsuffix方法获取真实 TLD 或伪 TLD。

use feature qw( say );

use Domain::PublicSuffix qw( );
use URI                  qw( );

my $dps = Domain::PublicSuffix->new();

for (qw(
   http://www.google.com/
   http://www.google.co.uk/
)) {
   my $url = $_;

   # Treat relative URLs as absolute URLs with missing http://.
   $url = "http://$url" if $url !~ /^\w+:/;

   my $host = URI->new($url)->host();
   $host =~ s/\.\z//;  # D::PS doesn't handle "domain.com.".

   $dps->get_root_domain($host)
      or die $dps->error();

   say $dps->tld();     # com  uk
   say $dps->suffix();  # com  co.uk
}
于 2011-11-07T00:52:47.903 回答