简体   繁体   中英

JSoup Selector internal text

I need to get " Text Here" and not "Paragraph" only using selectors How do i do it only using selector for JSoup?

<div>
   Text Here
   <p>Paragraph</p>
</div>

You can try to get the requested element using "getElemntById()". Here is an example:

String html="<html><body><div id='div1'>Text Here<p>Paragraph</p></div></body></html>";

Document doc = Jsoup.parse(html);
Element div = doc.getElementById("div1");
String str = div.ownText();

System.out.println(str);
public static void main(String... args) throws IOException {

    Document document = Jsoup.parse("<div>Text Here<p>Paragraph</p></div>");

    Element elem = document.select("div p").first();
    String text = elem.text();

    System.out.println(text);
}

Output

Paragraph

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM