2013-07-13 55 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 
相關問題