JSoup - 选择所有评论

1 java comments screen-scraping extract jsoup

我想使用JSoup从文档中选择所有注释.我想做这样的事情:

for(Element e : doc.select("comment")) {
   System.out.println(e);
}
Run Code Online (Sandbox Code Playgroud)

我试过这个:

for (Element e : doc.getAllElements()) {
  if (e instanceof Comment) {

  }
Run Code Online (Sandbox Code Playgroud)

}

但是在eclipse"不兼容的条件操作数类型元素和注释"中发生以下错误.

干杯,

皮特

dog*_*ane 11

因为Comment extends Node您需要应用于instanceof节点对象,而不是元素,如下所示:

    for(Element e : doc.getAllElements()){
        for(Node n: e.childNodes()){
            if(n instanceof Comment){
                System.out.println(n);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)