Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get this text using Jsoup?

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.

like image 656
wildnux Avatar asked Feb 23 '12 16:02

wildnux


1 Answers

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.

like image 118
BalusC Avatar answered Sep 28 '22 07:09

BalusC