繁体   English   中英

使用select时Jsoup忽略嵌套标签

[英]Jsoup ignore nested tags when using select

我正在尝试解析一个使用以下内容的网站

 <b>Header</b>Data<strong>Header</strong>Data

所以我有一个选择器是

.select("b, strong") 

然后尝试提取之间的文本。 - 一切顺利。

问题:有时站点有例如。

<strong><strong>HeaderX</strong><br /></strong>Data

现在这使我的循环变得混乱,因为我将两次获得文本headerX,如何忽略嵌套的强?

更新#1已解决,但可能有更好的方法。

Elements selected = info.select("b, strong");
Element next = selected.get(0);
Element now = null;
for (int i = 0; next != null ;i++) {
    now = next;
    next = null;
    Elements children = now.getAllElements();
    for (;selected.size() > i; i++) {
        next = selected.get(i);
        if (!children.contains(next)) {
            break;
        }
    }
    //Do whatever with now & next
}

试试这个

编辑

  info.select("b,strong").remove().text();

你可以尝试一下:

doc.select("strong > strong, strong:last-child");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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