使用 Remove() 方法可以删除某个子元素,例如:

package main

import (
    "fmt"
    "github.com/gocolly/colly"
)

func main() {
    c := colly.NewCollector()

    c.OnHTML("ul", func(e *colly.HTMLElement) {
        // 删除第一个 li 元素
        e.ChildText('li:first-child')
        e.DOM.Find('li:first-child').Remove()
        fmt.Println(e.Text)
    })

    c.Visit("https://example.com")
}

上面的代码会访问 https://example.com 页面,找到第一个 ul 元素,删除其中的第一个 li 元素,然后打印剩余的文本。

Go Colly: 如何删除子元素 (示例代码)

原文地址: https://www.cveoy.top/t/topic/ocg6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录