<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 =&#34; http://epaper.thedailystar.net/images/edailystar.png" 在android中使用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 :(得分:2)
由于您未提及url
,我认为url
为http://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 :(得分:2)
您必须遍历元素才能选择适合您需求的元素。像这样:
Elements elements = document.getElementsByTag("img");
for (Element element : elements) {
if (element.attr("src").endsWith("png")) {
System.out.println(element.attr("src"));
}
}