gpt4 book ai didi

java - 将 XPath 转换为 CSS 选择器

转载 作者:行者123 更新时间:2023-12-02 04:22:00 26 4
gpt4 key购买 nike

我正在使用 Jsoup 抓取页面中的数据,但数据不在特定标签中。

<strong>LABEL IS HERE</strong> DATA IS HERE

使用 XPath 我可以获得路径 //*[@id="center-text"]/text()[1]但不幸的是 chrome 不允许我复制 CSS 路径。

我可以获得 <strong> LABEL IS HERE</strong> 的 CSS 路径但不适用于其他文本。有没有办法使用 CSS 选择器语言获取这些数据?

<小时/>

示例数据

<div id="center-text"> 
<strong>ifno</strong> data&nbsp;&nbsp;&nbsp;
<strong>ifno</strong> data&nbsp;&nbsp;&nbsp;
<strong>Tifno</strong> data
<br>
<strong>ifno</strong> data&nbsp;&nbsp;&nbsp;
<strong>ifno</strong> data&nbsp;&nbsp;&nbsp;
<strong>ifno</strong> data
</div>

最佳答案

在 JSOUP 中您可以使用 nextSibling方法:

public Node nextSibling()

Get this node's next sibling.

Returns: next sibling, or null if this is the last sibling

你应该在以下时间离开:

Elements elements = doc.select("div[id=\"center-text\"] strong");

for(Element element : elements) {
System.out.println("nextSibling: " + element.nextSibling());
}

结果将是:

nextSibling:  data&nbsp;&nbsp;&nbsp;
nextSibling: data&nbsp;&nbsp;&nbsp;
nextSibling: data
nextSibling: data&nbsp;&nbsp;&nbsp;
nextSibling: data&nbsp;&nbsp;&nbsp;
nextSibling: data

关于java - 将 XPath 转换为 CSS 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32672945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com