2010-10-17 64 views
4

我需要從遠程網站檢索不提供RSS源的文本。如何在Google App Engine上執行網頁抓取以在Java中查找特定的鏈接頁面?

我知道的是,我需要的數據總是在鏈接到主頁面(http://www.example.com/)的頁面上,鏈接中包含文本「Invoices Report」。

例如:

<a href="http://www.example.com/data/invoices/2010/10/invoices-report---tuesday-october-12.html">Invoices Report - Tuesday, October 12</a> 

所以,我需要找到所有的主頁上符合這種模式,然後檢索所有文本從位於一個名爲<div class="invoice-body"> 標籤內的那些頁面的鏈接。

是否有的Java工具,這方面的幫助,是有什麼特別爲谷歌應用程序引擎的Java,可以用來做呢?

回答

4

退房http://code.google.com/appengine/docs/java/urlfetch/overview.html

可以使用網址抓取服務來讀取www.example.com/index.html行由行,並使用正則表達式來尋找「發票報告」。

URL url = new URL("http://www.example.com/index.html"); 
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); 
String line; 

while ((line = reader.readLine()) != null) { 
    checkLineForTextAndAddLinkOrWhatever(line); 
} 
reader.close(); 

如果鏈接可能位於多行,則可能需要其他種類的閱讀器。

相關問題