Jsoup-选择标签时出现问题 [英] Jsoup - Problem selecting a tag

查看:77
本文介绍了Jsoup-选择标签时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法选择一个在另一个元素之后的元素?

Is there a way to select an element following another one?

例如,如果我有:

<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);

但是我该怎么说:选择第一个之后的td?

But how can I do to say : select the td that follow the first th?

谢谢您的帮助.

推荐答案

您可以使用组合的 JSoup选择器.

例如,对于您的问题:选择第一个后的td"

For instance, for your question: "select the td that follow the first th"

  • lt伪选择器:

  • lt pseudo selector:

  • :lt(n)-同级索引小于n的元素
  • :lt(n) - elements whose sibling index is less than n

兄弟选择器:您有两个选择:

Sibling selector: you have two options:

  • E + F-紧随同级E的F元素
  • E ~ F-一个以E开头的F元素
  • E + F - an F element immediately preceded by sibling E
  • E ~ F - an F element preceded by sibling E

因此,对于选择第一个,它将为th:lt(1),对于紧随其后的td为+ td.

So, for selecting the first th it would be th:lt(1), and for the td that follows it + td.

最终代码:

Elements select = Jsoup.parse(HTML_PAGE).select("th:lt(1) + td");
Element element = select.get(0);

返回的元素将是td标签,紧随其后的是第一个找到的th标签.

The element returned will be the td tag immediately preceded by the first th tag found.

这篇关于Jsoup-选择标签时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆