Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting certain text after tag with jsoup

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.

like image 868
Yoda066 Avatar asked Nov 02 '22 07:11

Yoda066


1 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));
like image 109
Liviu Stirb Avatar answered Nov 13 '22 22:11

Liviu Stirb