如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)
}