登录
首页 >  Golang >  Go问答

使用 colly 迭代 HTMLElement 属性?

来源:stackoverflow

时间:2024-04-26 15:48:26 350浏览 收藏

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

问题内容

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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,我们可以迭代:

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

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

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>