Iterate over HTMLElement attributes with colly?

huangapple go评论113阅读模式
英文:

Iterate over HTMLElement attributes with colly?

问题

如在HTML结构中所见,attributes是一个私有属性:

  1. // HTMLElement是HTML标签的表示。
  2. type HTMLElement struct {
  3. // Name是标签的名称
  4. Name string
  5. Text string
  6. attributes []html.Attribute
  7. // Request是元素的HTML文档的请求对象
  8. Request *Request
  9. // Response是元素的HTML文档的响应对象
  10. Response *Response
  11. // DOM是页面的goquery解析的DOM对象。DOM是相对于当前HTMLElement的
  12. DOM *goquery.Selection
  13. // Index存储当前元素在由OnHTML回调匹配的所有元素中的位置
  14. Index int
  15. }

有一些函数,比如.Attr()用于获取单个属性,但是如何遍历所有属性呢?似乎没有明显的方法来访问attributes或该结构的函数。

英文:

As seen in the HTML struct, the attributes is a private property:

  1. // HTMLElement is the representation of a HTML tag.
  2. type HTMLElement struct {
  3. // Name is the name of the tag
  4. Name string
  5. Text string
  6. attributes []html.Attribute
  7. // Request is the request object of the element's HTML document
  8. Request *Request
  9. // Response is the Response object of the element's HTML document
  10. Response *Response
  11. // DOM is the goquery parsed DOM object of the page. DOM is relative
  12. // to the current HTMLElement
  13. DOM *goquery.Selection
  14. // Index stores the position of the current element within all the elements matched by an OnHTML callback
  15. Index int
  16. }

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,我们可以进行迭代:

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

By accessing the raw html.Node underneath, we can iterate:

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

huangapple
  • 本文由 发表于 2022年7月27日 08:39:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/73131106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定