Gabriel
Gabriel

Reputation: 5723

Getting attribute value with Go Colly

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" class="card card--featured" data-item-card="11042036">

Upvotes: 1

Views: 2779

Answers (1)

Amirreza Noori
Amirreza Noori

Reputation: 1525

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

Upvotes: 1

Related Questions