2013-01-03 61 views
3

我有一個問題,我很難研究,因爲我不知道如何在搜索引擎上正確提問。從URL列表中刪除只有頂級目錄的URL?

我有一個URL列表。我想要一些自動化的方式(首選Perl)來遍歷列表並刪除所有僅位於頂級目錄的URL。

因此,例如我可能有這樣的名單:

http://www.example.com/hello.html
http://www.foo.com/this/thingrighthere.html

在這種情況下,我想從我的列表中刪除example.com,因爲它要麼是頂級目錄或只他們引用頂級目錄中的文件。

我想弄清楚如何做到這一點。我的第一個想法是,計算正斜槓,如果有兩個以上,則從列表中刪除URL。但是,你的前斜線後跟,所以這是行不通的。

任何想法或想法將不勝感激。

+1

'http:// example.com/foo /'下降的方式是?注意尾部斜線。 – Schwern

回答

5

事情是這樣的:

use URI::Split qw(uri_split); 
my $url = "http://www.foo.com/this/thingrighthere.html"; 
my ($scheme, $auth, $path, $query, $frag) = uri_split($url); 
if (($path =~ tr/\///) > 1) { 
    print "I care about this $url"; 
} 

http://metacpan.org/pod/URI::Split

+0

謝謝阿奎那。我可以閱讀和翻譯。 : - > – user1946684

1

從CPAN使用URI模塊。 http://search.cpan.org/dist/URI

這是一個解決的問題。人們已經編寫,測試和調試過的代碼。無論何時您遇到其他人可能需要處理的編程問題,請查找現有的代碼,以便爲您解決問題。

+1

謝謝安迪。我認爲會有解決方案,但我不知道如何提出問題來找到它。 – user1946684

4

可以用正則表達式做到這一點,但它少得多的工作,讓URI庫爲你做。在路徑之前和之後(查詢,錨點,授權...),您都不會被有趣的方案,轉義和額外的東西所困擾。繞path_segments()表示路徑的方式存在一些問題。有關詳細信息,請參閱下面的註釋和the URI docs

我假設http://www.example.com/foo/被視爲頂級目錄。必要時進行調整,但必須考慮一些事項。

#!/usr/bin/env perl 

use URI; 
use File::Spec; 

use strict; 
use warnings; 

use Test::More 'no_plan'; 

sub is_top_level_uri { 
    my $uri = shift; 

    # turn it into a URI object if it isn't already 
    $uri = URI->new($uri) unless eval { $uri->isa("URI") }; 

    # normalize it 
    $uri = $uri->canonical; 

    # split the path part into pieces 
    my @path_segments = $uri->path_segments; 

    # for an absolute path, which most are, the absoluteness will be 
    # represented by an empty string. Also /foo/ will come out as two elements. 
    # Strip that all out, it gets in our way for this purpose. 
    @path_segments = grep { $_ ne '' } @path_segments; 

    return @path_segments <= 1; 
} 

my @filtered_uris = (
    "http://www.example.com/hello.html", 
    "http://www.example.com/", 
    "http://www.example.com", 
    "https://www.example.com/", 
    "https://www.example.com/foo/#extra", 
    "ftp://www.example.com/foo", 
    "ftp://www.example.com/foo/", 
    "https://www.example.com/foo/#extra", 
    "https://www.example.com/foo/?extra", 
    "http://www.example.com/hello.html#extra", 
    "http://www.example.com/hello.html?extra", 
    "file:///foo", 
    "file:///foo/", 
    "file:///foo.txt", 
); 

my @unfiltered_uris = (
    "http://www.foo.com/this/thingrighthere.html", 
    "https://www.example.com/foo/bar", 
    "ftp://www.example.com/foo/bar/", 
    "file:///foo/bar", 
    "file:///foo/bar.txt", 
); 

for my $uri (@filtered_uris) { 
    ok is_top_level_uri($uri), $uri; 
} 

for my $uri (@unfiltered_uris) { 
    ok !is_top_level_uri($uri), $uri; 
} 
+1

很好的回答。我的+1。 – aquinas

+2

謝謝Schwern!我會努力適應這個目的。我非常感謝您爲此付出的時間。 – user1946684

+1

你很受歡迎。 :) – Schwern