Jsoup select text after tag

I want to extract the text after each tag using jsoup. Is there a way to select it directly or do I need to execute .substring as a whole?

<div> <a href="#"> I don't want this text </a> **I want to retrieve this text** </div> 
+7
source share
3 answers
 public static void main(String... args) throws IOException { Document document = Jsoup.parse("<div>" + "<a href=\"#\"> I don't want this text </a>" + "**I want to retrieve this text**" + "</div>"); Element a = document.select("a").first(); Node node = a.nextSibling(); System.out.println(node.toString()); } 

Exit

 **I want to retrieve this text** 
+22
source

Yes, you can.

  • first enter html <div> and then select its html using .html()
  • get <a> element and get its html
  • get the length of the <a> html element
  • exclude the first part.
0
source

I think that the answer above does not correspond to universality, despite the fact that it offers a resolving direction.

nextSibling() not used when changing the html structure.

When I referenced the Jsoup api, I found a method called textNodes() that can get a list of node text from this element.

 public static String getTextAfterTag(Element ele) { String text = ""; for(TextNode node: ele.textNodes()) { text += node.text(); } return text; } 

hope to help.

0
source

All Articles