问题
How to get the text which is not part of any element?
<br><b>Price:</b> Rs. 24,900.00 <br>
Here, how can one get the text Rs.24,900.00. Is this possible using jsoup?
回答1:
I suppose there is a parent element so you should select that first and after just select the "b" like the following code. Basically just find the element in front of your text.
Document doc = Jsoup.parse( "<br><b>Price:</b> Rs. 24,900.00 <br>");
Element el = doc.select("b").first();
String text = ((TextNode) el.nextSibling()).text();
I used first because I knew from your example that there is only one "b" element. In case you have multiple prices you have to iterate over all elements instead of using first.
Jsoup stores text as nodes. So nextSibling will return a node (TextNode) that follows after the "b" element and contains text value: " Rs. 24,900.00 "
来源:https://stackoverflow.com/questions/29251045/how-to-get-text-which-is-not-part-of-any-element-using-jsoup