gpt4 book ai didi

go - GoLang WebCrawling删除空标签

转载 作者:行者123 更新时间:2023-12-01 22:23:39 25 4
gpt4 key购买 nike

我正在尝试抓取网络,在少数情况下,我的HTML看起来像这样,其中包括\n\t


<article>
<div></div>
<p>
<br/>\n</p>\n\t
<p><span></span></p>
</article>


在某些情况下,如果我也需要删除 \n\t,如何删除标签。
  • 剥离所有\ n,以使标记变为空。
  • 如果为空,则其父级也将为空,这也需要递归剥离。
  • 最佳答案

    不知道这是不是你想要的

    re, _ := regexp.Compile("(<.*?>|\n|\t|\\\\n|\\\\t)")
    rep := re.ReplaceAllString(`<article>
    <div></div>
    <p>
    <br/>\n</p>\n\t
    <p><span></span></p>
    </article>`, "")
    fmt.Println(rep)

    或者,以下代码仅删除空标签。
    func RemoveTags(html string) string {
    re, _ := regexp.Compile("<[^>/]+></[^>]+>")
    rep := re.ReplaceAllString(html, "")
    if rep != html {
    return RemoveTags(rep)
    }
    return rep
    }

    re, _ := regexp.Compile("(\n|\t|\\\\n|\\\\t|<[^/>]+/>)")
    rep := re.ReplaceAllString(`<article>123
    <div></div>
    <p>
    <br/>\n</p>\n\t
    <p><span></span></p>
    </article>`, "")
    fmt.Println(RemoveTags(rep))

    结果:
    <article>123</article>

    关于go - GoLang WebCrawling删除空标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61329699/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com