我想从源HTML中删除那些带有内容的标记。
答案 0 :(得分:34)
搜索时,您基本上使用Elements.select(selector)
,其中selector
由this API定义。但是注释在技术上不是元素,因此您可能会在这里感到困惑,它们仍然是由节点名#comment
标识的节点。
让我们看看它是如何运作的:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
public class RemoveComments {
public static void main(String... args) {
String h = "<html><head></head><body>" +
"<div><!-- foo --><p>bar<!-- baz --></div><!--qux--></body></html>";
Document doc = Jsoup.parse(h);
removeComments(doc);
doc.html(System.out);
}
private static void removeComments(Node node) {
for (int i = 0; i < node.childNodesSize();) {
Node child = node.childNode(i);
if (child.nodeName().equals("#comment"))
child.remove();
else {
removeComments(child);
i++;
}
}
}
}
答案 1 :(得分:2)
使用JSoup 1.11+(可能是较旧的版本),您可以应用过滤器:
private void removeComments(Element article) {
article.filter(new NodeFilter() {
@Override
public FilterResult tail(Node node, int depth) {
if (node instanceof Comment) {
return FilterResult.REMOVE;
}
return FilterResult.CONTINUE;
}
@Override
public FilterResult head(Node node, int depth) {
if (node instanceof Comment) {
return FilterResult.REMOVE;
}
return FilterResult.CONTINUE;
}
});
}
答案 2 :(得分:1)
引用@dlamblin https://stackoverflow.com/a/7541875/4712855此代码获取html注释
public static void getHtmlComments(Node node) {
for (int i = 0; i < node.childNodeSize();i++) {
Node child = node.childNode(i);
if (child.nodeName().equals("#comment")) {
Comment comment = (Comment) child;
child.after(comment.getData());
child.remove();
}
else {
getHtmlComments(child);
}
}
}
答案 3 :(得分:1)
这是使用函数编程方法的第一个示例的变体。查找所有注释(它们是当前节点的直接子代)的最简单方法是在.filter()
.childNodes()
public void removeComments(Element e) {
e.childNodes().stream()
.filter(n -> n.nodeName().equals("#comment")).collect(Collectors.toList())
.forEach(n -> n.remove());
e.children().forEach(elem -> removeComments(elem));
}
完整示例:
package demo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.stream.Collectors;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Demo {
public static void removeComments(Element e) {
e.childNodes().stream()
.filter(n -> n.nodeName().equals("#comment")).collect(Collectors.toList())
.forEach(n -> n.remove());
e.children().forEach(elem -> removeComments(elem));
}
public static void main(String[] args) throws MalformedURLException, IOException {
Document doc = Jsoup.parse(new URL("https://en.wikipedia.org/"), 500);
// do not try this with JDK < 8
String userHome = System.getProperty("user.home");
PrintStream out = new PrintStream(new FileOutputStream(userHome + File.separator + "before.html"));
out.print(doc.outerHtml());
out.close();
removeComments(doc);
out = new PrintStream(new FileOutputStream(userHome + File.separator + "after.html"));
out.print(doc.outerHtml());
out.close();
}
}
答案 4 :(得分:-2)
这很好,这段代码工作
doc.select("#comment").remove();
并通过代码删除许多标签
doc.select("script, style, meta, link, comment, CDATA, #comment").remove();