2014-03-06 49 views
-1

我從html頁面提取數據,然後解析包含這樣標籤的標籤,現在我嘗試了像提取子字符串等不同的方法,只提取標題和href標籤。但它不工作......任何人都可以幫助我。這是我的輸出使用java從html標記中提取內容

我的代碼

 doc = Jsoup.connect("myurl").get(); 

    Elements link = doc.select("a[href]"); 
    String stringLink = null; 
    for (int i = 0; i < link.size(); i++) 
    { 

     stringLink = link.toString(); 
     System.out.println(stringLink); 
    } 

輸出

<a class="link" title="Waf Ad" href="https://www.facebook.com/waf.ad.54" 
data- jsid="anchor" target="_blank"><img class="_s0 _rw img" src="https: 
//fbcdn-profile-a.akamaihd.net/hprofile-ak-ash1/t5/186729_100007938933785_ 
508764241_q.jpg" alt="Waf Ad" data-jsid="img" /></a> 
<a class="link" title="Ana Ga" href="https://www.facebook.com/ata.ga.31392410" 
data-jsid="anchor" target="_blank"><img class="_s0 _rw img" src="https:// 
fbcdn-profile-a.akamaihd.net/hprofile-ak-ash1/t5/186901_100002334679352_ 
162381693_q.jpg" alt="Ana Ga" data-jsid="img" /></a> 
+1

代碼片段提取選擇性內容使用jsoup也將有所幫助(它的標籤在jsoup下) – PopoFibo

+0

@PopoFibo我試過了..但它不工作我怎麼才能得到只有標題內容和href內容 – chopu

+1

即使如此,顯示你的努力(即你的代碼)表示讚賞,即使這種努力沒有成果 – fge

回答

3

獲得頁面的標題,您可以使用

Document doc = Jsoup.connect("myurl").get(); 
String title = doc.title(); 

對於從不同的HREFs獲得單個鏈接,你可以使用這個

Elements links = doc.select("a[href]"); 
for(Element ele : links) { 
    System.out.println(ele.attr("href").toString()); 
} 

attr()方法將給定標籤中匹配歸因的內容加到其中。

+0

對不起,沒有在標籤中看到標題。爲此,正如@ashatte建議的那樣,可以使用'attr(「title」)'從單個標籤中獲取它。 – Rakesh

+0

感謝rakesh我越來越href的 – lulu