程序开发 · 2023年12月4日

使用 colly 迭代 HTMLElement 属性?

当前位置: > > > > 使用 colly 迭代 HTMLElement 属性?

来源:stackoverflow
2024-04-26 15:48:26
0浏览
收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《使用 colly 迭代 HTMLElement 属性?》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

如 html 结构中所示,属性是私有属性:

// HTMLElement is the representation of a HTML tag.
type HTMLElement struct {
    // Name is the name of the tag
    Name       string
    Text       string
    attributes []html.Attribute
    // Request is the request object of the element's HTML document
    Request *Request
    // Response is the Response object of the element's HTML document
    Response *Response
    // DOM is the goquery parsed DOM object of the page. DOM is relative
    // to the current HTMLElement
    DOM *goquery.Selection
    // Index stores the position of the current element within all the elements matched by an OnHTML callback
    Index int
}

有诸如 .attr() 之类的函数用于获取单个属性,但是我如何迭代所有属性?似乎没有明显的方法来访问该结构中的 attributes 或函数。

正确答案

通过访问下面的原始 html.node,我们可以迭代:

for _, node := range e.DOM.Nodes {
    fmt.Println(node.Attr)
}

以上就是《使用 colly 迭代 HTMLElement 属性?》的详细内容,更多关于的资料请关注公众号!