使用 Go Colly 获取属性值

发布于 2025-01-17 03:16:39 字数 381 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2025-01-24 03:16:39

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"))
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文