2013-07-13 57 views
-1

http://packdeps.haskellers.com/reverse获取内容根据第二列排序表示此表的内容的最简单方法是什么?哪种工具最适合这种工作?对html表格内容进行排序的最简单方法

由于内容看起来很简单,我试图用tr,sed和awk破解(主要是为了学习这些工具),但结果太复杂,无法正确地获取所有行。格式可能看起来像这样:

47 strict 
54 Win32 
55 transformers-base 
57 enumerator 
68 system-filepath 
69 xml 

或任何其他格式,只要它不作进一步处理太复杂。

回答

0

我喜欢,只是学习我没有使用Web::Scraper模块的工作。它使用CSS选择器来提取表中的列和由第二个,其指示dependencias每个包的数目将它们排序:

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}; 
} 

运行它提供的网址:

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 
相关问题