2013-07-01 33 views
3

我有一個文本文件,其中包含多個帶有URL其他信息的URL。我如何才能讀取txt文件並將URL保存在數組中才能下載?我想用如何從MATLAB中的txt文件中只讀URL

C = textscan(fileId, formatspec); 

我應該在formatspec中提及URL格式嗎?

+0

我不是java精明的,但我想你可以在Matlab中使用java做到這一點,你可以從閱讀[*如何檢測字符串中URL的存在*]開始(http://stackoverflow.com/questions/) 285619/how-to-detect-the-presence-of-url-in-a-string)和[*從MATLAB *調用Java](http://blogs.mathworks.com/community/2009/07/06/調用Java的 - 從 - MATLAB /)。 – pm89

回答

4

這不是textscan的工作;你應該使用regular expressions這個。在MATLAB中,正則表達式描述爲here。 有關URL,請參閱herehere以獲取其他語言的示例。

下面是一個例子在MATLAB:

% This string is obtained through textscan or something 
str = {... 
    'pre-URL garbage http://www.example.com/index.php?query=test&otherStuf=info more stuff here' 
    'other foolish stuff ftp://localhost/home/ruler_of_the_world/awesomeContent.py 1 2 3 4 misleading://'; 
}; 


% find URLs  
C = regexpi(str, ... 
    ['((http|https|ftp|file)://|www\.|ftp\.)',... 
    '[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]'], 'match'); 

C{:} 

結果:

ans = 
    'http://www.example.com/index.php?query=test&otherStuf=info' 
ans = 
    'ftp://localhost/home/ruler_of_the_world/awesomeContent.py' 

注意,這個正則表達式需要你有協議包括,擁有國內領先的www.ftp.。類似example.com/universal_remote.cgi?redirect=不是匹配。

你可以繼續並使正則表達式覆蓋越來越多的案例。然而,最終你會偶然發現最重要的結論(例如here,例如我得到了我的正則表達式):給定完整的定義了什麼確切構成了有效的URL,沒有單個正則表達式到總是匹配有效的URL。也就是說,您可以設想的有效網址是而不是,這些網址是由任何顯示的正則表達式捕獲的。

但請記住,這最後一個陳述更理論化而非實際 - 這些不匹配的URL是有效的,但在實踐中並不經常遇到:)換句話說,如果您的URL具有非常標準的表單,那麼您幾乎覆蓋了我給你的正則表達式。

現在,我在pm89的Java建議中有些蠢蠢欲動。正如我懷疑的那樣,它比一個正則表達式慢了一個數量級,因爲你在代碼中引入了另一個「goo層」(在我的時間點上,差別大約慢了40倍,不包括進口)。這裏是我的版本:

import java.net.URL; 
import java.net.MalformedURLException; 

str = {... 
    'pre-URL garbage http://www.example.com/index.php?query=test&otherStuf=info more stuff here' 
    'pre--URL garbage example.com/index.php?query=test&otherStuf=info more stuff here' 
    'other foolish stuff ftp://localhost/home/ruler_of_the_world/awesomeContent.py 1 2 3 4 misleading://'; 
}; 


% Attempt to convert each item into an URL. 
for ii = 1:numel(str)  
    cc = textscan(str{ii}, '%s'); 
    for jj = 1:numel(cc{1}) 
     try 
      url = java.net.URL(cc{1}{jj}) 

     catch ME 
      % rethrow any non-url related errors 
      if isempty(regexpi(ME.message, 'MalformedURLException')) 
       throw(ME); 
      end 

     end 
    end 
end 

結果:

url = 
    'http://www.example.com/index.php?query=test&otherStuf=info' 
url = 
    'ftp://localhost/home/ruler_of_the_world/awesomeContent.py' 

我不是太熟悉的java.net.URL,但顯然,它也無法找到沒有領先的協議或標準網域網址(例如,example.com/path/to/page) 。

這個片段無疑能夠加以改進,但我懷疑你可以使用java.net.URL我會勸你要考慮你爲什麼會想長此這樣做,本身慢,遠醜陋的解決方案:)

+0

+1:好的學習和好的解決方案。然而,我不認爲如果沒有標準方案(例如'example.com/path/to/page'),會有一種快速檢測方法,因爲唯一的方法就是需要連接到服務器並檢查連接是否爲[this回答](http://stackoverflow.com/a/1600333/1698972)建議。 – pm89

3

根據this answer

要實現相同的代碼在Matlab的

首先文件讀入到一個字符串,使用fileread例如:

str = fileread('Sample.txt'); 

再拆相對於空間的文本,用strsplit

spl_str = strsplit(str); 

最後用java.net.URL來檢測UR可以使用urlwrite將URL的內容寫入文件中。但是,首先從java.net.URL獲得的URL轉換到char

url = java.net.URL(spl_str{k}); 
urlwrite(char(url), 'test.html'); 

希望它能幫助。