How do I get this text using Jsoup?

无人久伴 提交于 2019-12-23 09:53:48

问题


How do i get "this text" from the following html code using Jsoup?

<h2 class="link title"><a href="myhref.html">this text<img width=10 
        height=10 src="img.jpg" /><span class="blah">
        <span>Other texts</span><span class="sometime">00:00</span></span>
        </a></h2>

When I try

String s = document.select("h2.title").select("a[href]").first().text();

it returns

this textOther texts00:00

I tried to read the api for Selector in Jsoup but could not figure out much.

Also how do i get an element of class class="link title blah" (multiple classes?). Forgive me I only know both Jsoup and CSS a little.


回答1:


Use Element#ownText() instead of Element#text().

String s = document.select("h2.link.title a[href]").first().ownText();

Note that you can select elements with multiple classes by just concatenating the classname selectors together like as h2.link.title which will select <h2> elements which have at least both the link and title class.



来源:https://stackoverflow.com/questions/9417295/how-do-i-get-this-text-using-jsoup

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