【问题标题】:How to remove a specific tag from the entire html page using jsoup如何使用jsoup从整个html页面中删除特定标签
【发布时间】:2014-01-11 20:00:05
【问题描述】:

我正在使用 jsoup 1.7.3 来编辑一些 html 文件。

我需要从 html 文件中删除以下标签:

<meta name="GENERATOR" content="XXXXXXXXXXXXXX">
<meta name="CREATED" content="0;0">
<meta name="CHANGED" content="0;0">

当你看到它的标签时,我该怎么做,这是我迄今为止尝试过的:

//im pretty sure that the <meta> tag is nested in the <header>
but removing the whole  header is bad practice.

Document docsoup = Jsoup.parse(htmlin);
docsoup.head().remove();

你有什么建议?

【问题讨论】:

    标签: java html dom tags jsoup


    【解决方案1】:

    我建议你使用Jsoup selectors,例如

    Document document = Jsoup.parse(html);
    Elements selector = document.select("meta[name=GENERATOR]");
    
    for (Element element : selector) {
        element.remove();
    }
    
    doc.html(); // returns String html with elements removed
    

    【讨论】:

    • 这可以在 1 行中完成:Jsoup.parse(html).select("script,hidden,style,form").remove().html()
    猜你喜欢
    • 2016-04-06
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2012-12-06
    相关资源
    最近更新 更多