A
A
Aylix2019-10-31 22:07:10
Java
Aylix, 2019-10-31 22:07:10

How to use jsoup to select only one span out of 2 in a container div?

html code

<div title class="Example">
<span>first div</span> <!----> 
<span class="second div">second span</span></div>

Java code
Document doc = Jsoup.connect("example.com").get();
        Elements elemenx = doc.select("div.Example span");
        for (Element e: elemenx) {
            System.out.println(e.text());
        }

Can't get only the content of the first span<span>first div</span>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aylix, 2019-10-31
@Aylix

Found a solution with nth-child
Elements elemenx = doc.select("div.Example span:nth-child(1)");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question