使用Go Colly获取属性值

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

Getting attribute value with Go Colly

问题

当使用c.OnHTML在"html"中工作时,如何获取#id-card-1 ID内href属性的值?

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

这是页面中的HTML片段:

英文:

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

答案1

得分: 1

ChildAttr函数可以用于此目的。

> ChildAttr返回第一个匹配元素的属性的剥离文本内容。

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"))
})
英文:

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

huangapple
  • 本文由 发表于 2022年3月27日 02:45:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/71630862.html
匿名

发表评论

匿名网友

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

确定