英文:
Iterate over HTMLElement attributes with colly?
问题
如在HTML结构中所见,attributes
是一个私有属性:
// HTMLElement是HTML标签的表示。
type HTMLElement struct {
// Name是标签的名称
Name string
Text string
attributes []html.Attribute
// Request是元素的HTML文档的请求对象
Request *Request
// Response是元素的HTML文档的响应对象
Response *Response
// DOM是页面的goquery解析的DOM对象。DOM是相对于当前HTMLElement的
DOM *goquery.Selection
// Index存储当前元素在由OnHTML回调匹配的所有元素中的位置
Index int
}
有一些函数,比如.Attr()
用于获取单个属性,但是如何遍历所有属性呢?似乎没有明显的方法来访问attributes
或该结构的函数。
英文:
As seen in the HTML struct, the attributes is a private property:
// 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
}
There are functions such a .Attr()
for fetching a single attribute, but how would I iterate over all attributes? It seems that there is no obvious way to access attributes
or functions from that struct.
答案1
得分: 1
通过访问底层的html.Node
,我们可以进行迭代:
for _, node := range e.DOM.Nodes {
fmt.Println(node.Attr)
}
英文:
By accessing the raw html.Node
underneath, we can iterate:
for _, node := range e.DOM.Nodes {
fmt.Println(node.Attr)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论