我想获取图片名称及其图片网址。我的HTML是这样的:
<td class="text-center">
<a href="MYLINK.html">
<img src="IMAGE INK.jpg" alt="IMAGE NAME" title="MY TITILE" class="img-thumbnail" />
</a>
</td>
我该怎么做?
答案 0 :(得分:2)
获得图像元素后,例如:
Element image = document.select("img").first();
String url = image.attr("abs:src");
String name = image.attr("abs:title");
或尝试一起玩,看看你想要的信息:
.attr("src")
.attr("title")
答案 1 :(得分:1)
String your_html = "<td class=\"text-center\"><a href=\"MYLINK.html\"><img src=\"IMAGE INK.jpg\" alt=\"IMAGE NAME\" title=\"MY TITILE\" class=\"img-thumbnail\" /></a></td>";
Document document = Jsoup.parseBodyFragment(html);
Element element = document.body();
Element link = element.select("img.img-thumbnail").first(); // Img with class img-thumbnail
System.out.println(link.attr("src")); //The attribute, 'src' , within the selected img tag
希望这会有所帮助:)
答案 2 :(得分:1)
我是通过
完成的 Element e4 = row.select("td.text-center > a > img").first();
String URL = e4.attr("src");
String TITLE = e4.attr("title");
System.out.println("URL = " + URL);
System.out.println("TITLE = " + TITLE);
答案 3 :(得分:0)
这就是我做的方式。
String html_to_parse = "<td class=\"text-center\"><a href=\"MYLINK.html\"><img src=\"IMAGE INK.jpg\" alt=\"IMAGE NAME\" title=\"MY TITILE\" class=\"img-thumbnail\" /></a></td>";
Document doc = Jsoup.parse(html_to_parse);
String imageUrl = doc.select("img").attr("src");
String imageName = doc.select("img").attr("title");