2015-02-24 60 views
0
<div class='ym-gbox adds-header'> 
    <a href='javascript:(void);' > 
     <a href="http://epaper.thedailystar.net/" target="_blank"> 
     <img src="http://epaper.thedailystar.net/images/edailystar.png" alt="edailystar" style="float: left; width: 100px; margin-top: 15px;"> 
     </a> 
     <a href="http://www.banglalink.com.bd/celebrating10years" target="_blank" style="display:block;float: right;"> 
     <img width="490" height="60" src="http://bd.thedailystar.net/upload/ads/2015/02/12/BD-News_490x60.gif" alt="banglalink" > 
     </a> 
    </a> 
</div> 

這是html部分。從這裏我想提取圖像標籤的圖像源與源地址src =「http://epaper.thedailystar.net/images/edailystar.png」在android中使用jsoup。但我失敗了。如果有人給出答案,我會感謝他。從嵌套div中提取圖像源和JSOUP中的標記

這裏是我的代碼

Document document = Jsoup.connect(url).get(); 
Elements img = document.select("div[class=ym-gbox adds-header]").first().select("a[href=http://epaper.thedailystar.net/] > img[src]"); 
String imgSrc = img.attr("src"); 
+0

什麼是網址?所以我可以試試.. – 2015-02-24 19:08:45

回答

1

因爲你沒有提到url,我認爲urlhttp://epaper.thedailystar.net/index.php

Document doc = Jsoup.connect("http://epaper.thedailystar.net/index.php").timeout(10*1000).get(); 
Elements div = doc.select("div.logo"); 
Elements get = div.select("img"); 
System.out.println(get.attr("abs:src")); 

輸出:

http://epaper.thedailystar.net/images/edailystar.png 
1

你必須通過元素迭代選擇適合您需求的元素。像這樣:

Elements elements = document.getElementsByTag("img"); 

for (Element element : elements) { 
    if (element.attr("src").endsWith("png")) { 
     System.out.println(element.attr("src")); 
    } 
} 
+0

感謝您回覆Yuva Raj和Xryaznov。 – user3256010 2015-02-26 16:20:03

+0

對於Yuva Raj,您的代碼適用於**網址:http://epaper.thedailystar.net/index.php **。但是,當我使用**網址:http://www.thedailystar.net/ **它不起作用意味着它無法獲取圖像。你能告訴我爲什麼會發生這種情況嗎?我嘗試了所有的方法,但是如果'url'變化,'div'也會變化,所以 – user3256010 2015-02-26 16:21:10

+0

@ user3256010失敗。 – 2015-03-05 17:15:26