Home > Enterprise >  Getting attribute value with Go Colly
Getting attribute value with Go Colly

Time:03-27

When working with c.OnHTML in "html", how can I get the value of the href attribute inside the #id-card-1 ID?

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

This is the piece of HTML in the page:

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

CodePudding user response:

The ChildAttr function can use for this purpose.

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"))
})
  • Related