使用 Go Colly 获取属性值

Getting attribute value with Go Colly

在“html”中使用c.OnHTML时,如何获取#id-card-1 ID 中的 href 属性值?

   c.OnHTML("html", func(e *colly.HTMLElement) {
...
    linkStr := "#id-card-1[href]" //???
    log.Print(e.Attr(linkStr))
...}

这是页面中HTML的片段:

<a href="/some-link-here" target="_blank" id="id-card-1" class="card card--featured" data-item-card="11042036">

ChildAttr 函数可用于此目的。

ChildAttr returns the stripped text content of the first matching element's attribute.

https://pkg.go.dev/github.com/gocolly/colly#HTMLElement.ChildAttr

c.OnHTML("html", func(e *colly.HTMLElement) {
    linkStr := "#id-card-1"
    log.Println(e.ChildAttr(linkStr, "href"))
})