简体   繁体   中英

jSoup get title from img tag

I have a scenario where I need to pull the title from a img tag like below.

<img alt="Bear" border="0" src="/images/teddy/5433.gif" title="Bear"/>

I was able to get the image url. But how do i get the title from the img tag. From above title = "bear". I want to extract this.

Use Element#attr() to extract arbitrary element attributes.

Element img = selectItSomehow();
String title = img.attr("title");
// ...

See also:

String html = "<img alt='Bear' border='0' src='/images/teddy/5433.gif' title='Bear'/>";
Document doc = Jsoup.parse(html);
Element e = doc.select("img[title]").first();
String title = e.attr("title");
System.out.println(title);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM