2012-12-06 65 views
0

我只是盯着學習Jsoup和他們的網站上的食譜,但我只是有點卡住文本添加到我已解析的元素。在鏈接之前和之後添加文本jSoup

try{ 

      Document doc = Jsoup.connect(url).get();  
      Element add = doc.prependText("a href") ; 
      Elements links = add.select("a[href]");     

       for (Element link : links) {       


       PrintStream sb = System.out.format("%n %s",link.attr("abs:href")); 

       System.out.print("<br>"); 
             }  

    }  
    catch(Exception e){   
     System.out.print("error --> " + e); 
    } 

實例運行與google.com我得到

http://www.google.ie/imghp?hl=en&tab=wi<br> 
http://maps.google.ie/maps?hl=en&tab=wl<br> 
https://play.google.com/?hl=en&tab=w8<br> 

但我真的想

<a href> http://www.google.ie/imghp?hl=en&tab=wi<br></a> 
<a href> http://maps.google.ie/maps?hl=en&tab=wl<br></a> 
<a href> https://play.google.com/?hl=en&tab=w8<br></a> 

有了這個代碼,我已經得到過的頁面的所有鏈接,但我想也得到和標籤,所以我可以讓他們創建我的網頁。我已經嘗試添加一個字符串並預先添加文本,但似乎無法正確顯示。

感謝

回答

0

link.attr(...)你得到的屬性值

但你需要整個標籤:

Document doc = Jsoup.connect(...).get(); 


for(Element e : doc.select("a[href]")) // Select all 'a'-Tags with 'href' attribute 
{ 
    String wholeTag = e.toString(); // Get a string as the element is 

    /* No you you can use the html - in this example for a simple output */ 
    System.out.println(wholeTag); 
} 
+0

感謝,如果我用它打印的頁面圖像過於每一件事情,所以它只是使用更好的元素將它縮小到剛開的情況下,文本和href鏈接。 – JohnnyQ

+0

好的,你能發佈你想要選擇鏈接的HTML部分嗎?也許有些東西可以用來找到適當的鏈接。 – ollo

相關問題