jSoup get title from img tag

余生长醉 提交于 2020-01-05 09:02:24

问题


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.


回答1:


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

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

See also:

  • Jsoup Cookbook - Extract attributes, text, and HTML from elements



回答2:


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);


来源:https://stackoverflow.com/questions/9086644/jsoup-get-title-from-img-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!