我喜欢perl,只是为了学习,我使用该Web::Scraper
模块完成了这项工作。它使用CSS
选择器提取表的两列并按第二列排序,第二列表示每个包的依赖数:
script.pl
文件:
#!/usr/bin/env perl
use strict;
use warnings;
use Web::Scraper;
use URI;
die qq|Usage: perl $0 <url>\n| unless @ARGV == 1;
my $packages_deps = scraper {
process 'tr', 'package_deps[]' => scraper {
process 'td:first-child > a', 'package_name' => 'TEXT';
process 'td:nth-child(2)', 'tot_deps' => 'TEXT';
};
result 'package_deps';
};
my $response = $packages_deps->scrape( URI->new( shift ) );
for ( sort { $a->{tot_deps} <=> $b->{tot_deps} } @$response[1..$#$response] ) {
printf qq|%d %s\n|, $_->{tot_deps}, $_->{package_name};
}
运行它提供 url:
perl script.pl "http://packdeps.haskellers.com/reverse"
它产生(仅显示列表的开头和结尾部分):
1 abstract-par-accelerate
1 accelerate-fft
1 acme-year
1 action-permutations
1 active
1 activehs-base
...
766 text
794 filepath
796 transformers
915 directory
1467 mtl
1741 bytestring
1857 containers
5287 base