Hi I am trying to get certain text from HTML file using jsoup. I already know how to get text2 and text3. But how do I get text I want without others?
<div class="snt"> text I want
<br clear="both" />text2
<br clear="both" />text3
<br clear="both" />
</div>
I tried to use
Elements lines = doc.select(".snt");
lines.First().nextSibling().toString();
but I get nothing. I also tried:
Elements lines = doc.select(".snt");
lines.text(); // this return all texts together
Can you please help me? Thank you for your answers.
If you try ownText() for the first element you will get "text I want text2 text3" and this is correct. You want the text before br and this is the first child node under your first element. Jsoup handles text as a node.
Elements lines = doc.select(".snt");
System.out.println(lines.first().childNodes().get(0));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With