2012-02-21 45 views
-1
String html = Jsoup.connect("www.example.com").get().html(); 
Scanner in = new Scanner(html); 

String links ; 
while(in.hasNext()){ 
    String line = in.nextLine(); 
    if(line.contains("sometext")){ 

String links += line.substring(line.indexOf("http").line.indexOf("</a>") + "\n"); 

    } 

我有上面的代碼。它使用JSoup獲取網頁的HTML,之後,我想將它們保存爲一個字符串或一個由新行分隔的數組。這是我的問題。將解析的鏈接保存到一個字符串或一個數組java

回答

0

您應該繼續使用jsoup來檢索和解析HTML。另外,jsoup的文檔address this

String output = ""; 
// Get the webpage and parse it. 
Document doc = Jsoup.connect(url).get(); 
// Get the anchors with href attribute. 
// Or, you can use doc.select("a") to get all the anchors. 
Elements links = doc.select("a[href]"); 
// Iterate over all the links and process them. 
for (Element link : links) { 
    output += link.attr("abs:href"); 
} 
+0

謝謝。它工作正常....... – 2012-02-21 20:21:58

相關問題