簡體   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