문제
Is there a way to select an element following another one?
For example if I have :
<table>
<tr>
<th></th>
<td></td>
</tr>
<tr>
...
</tr>
</table>
and I want to select the first th I can do :
Elements select = Jsoup.parse(HTML_PAGE).select("th");
Element element = select.get(0);
But how can I do to say : select the td that follow the first th?
Thank you for your help.
해결책
You can use an combine different JSoup Selectors.
For instance, for your question: "select the td that follow the first th"
lt pseudo selector:
:lt(n)
- elements whose sibling index is less than n
Sibling selector: you have two options:
E + F
- an F element immediately preceded by sibling EE ~ F
- an F element preceded by sibling E
So, for selecting the first th it would be th:lt(1)
, and for the td that follows it + td
.
The final code:
Elements select = Jsoup.parse(HTML_PAGE).select("th:lt(1) + td");
Element element = select.get(0);
The element returned will be the td tag immediately preceded by the first th tag found.
제휴하지 않습니다 StackOverflow