Colly – 如何获取子属性的值?

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

Colly - How to get the value of a child attribute?

问题

这是我正在处理的示例页面 https://www.lazada.vn/-i1701980654-s7563711492.html

这是我想要获取的元素(产品标题):

...
<div>
   <img src="https://lzd-img-global.slatic.net/g/tps/imgextra/i1/O1CN01JUOYif22N3Uu7JX4R_!!6000000007107-2-tps-162-48.png" class="pdp-mod-product-badge" alt="LazMall">
    <h1 class="pdp-mod-product-badge-title">
     Yierku 【Free Shipping Miễn phí vận chuyển】Giày nam mùa thu và mùa đông giày thường xu hướng nam thể thao tất cả các trận đấu giày da tăng chiều cao giày nam
    </h1>
</div>
...

我想要获取 <h1> 元素之间的文本值,即 Yierku 【Free Shipping Miễn phí vận chuyển】Giày n....

这是我迄今为止尝试的代码:

    c := colly.NewCollector()
	c.OnError(func(_ *colly.Response, err error) {
		log.Println("Something went wrong:", err)
	})
	c.OnXML("/html/body", func(e *colly.XMLElement) {
		child := e.ChildAttrs("div[4]/div/div[3]/div[2]/div/div[1]/div[3]/div/div/h1", "class")
		fmt.Println(child)
		//fmt.Println(child)
	})

它返回了 pdp-mod-product-badge-title 的响应。

当我尝试将其更改为:

child := e.ChildAttrs("div[4]/div/div[3]/div[2]/div/div[1]/div[3]/div/div/h1", "text")

它没有给我任何结果。

英文:

Here is the sample page I been working on https://www.lazada.vn/-i1701980654-s7563711492.html

Here is the element I want to get (the product title)

...
<div>
   <img src="https://lzd-img-global.slatic.net/g/tps/imgextra/i1/O1CN01JUOYif22N3Uu7JX4R_!!6000000007107-2-tps-162-48.png" class="pdp-mod-product-badge" alt="LazMall">
    <h1 class="pdp-mod-product-badge-title">
     Yierku 【Free Shipping Miễn phí vận chuyển】Giày nam mùa thu và mùa đông giày thường xu hướng nam thể thao tất cả các trận đấu giày da tăng chiều cao giày nam
    </h1>
</div>
...

I want to get the text value between the <h1> element which is Yierku 【Free Shipping Miễn phí vận chuyển】Giày n....

Here is what I have tried so far

    c := colly.NewCollector()
	c.OnError(func(_ *colly.Response, err error) {
		log.Println("Something went wrong:", err)
	})
	c.OnXML("/html/body", func(e *colly.XMLElement) {
		child := e.ChildAttrs("div[4]/div/div[3]/div[2]/div/div[1]/div[3]/div/div/h1", "class")
		fmt.Println(child)
		//fmt.Println(child)
	})

It gives the response of pdp-mod-product-badge-title

When I tried to change it into

child := e.ChildAttrs("div[4]/div/div[3]/div[2]/div/div[1]/div[3]/div/div/h1", "text")

It does not give me any result

答案1

得分: 2

请使用func (*XMLElement) ChildText代替。

package main

import (
	"fmt"

	"github.com/gocolly/colly/v2"
)

func main() {
	c := colly.NewCollector()
	c.OnError(func(_ *colly.Response, err error) {
		fmt.Println("出错了:", err)
	})
	c.OnXML("/html/body", func(e *colly.XMLElement) {
		child := e.ChildText("div[4]/div/div[3]/div[2]/div/div[1]/div[3]/div/div/h1")
		fmt.Println(child)
	})
	c.Visit("https://www.lazada.vn/-i1701980654-s7563711492.html")
	// 输出:
	// Yierku 【Free Shipping Miễn phí vận chuyển】Giày nam mùa thu và mùa đông giày thường xu hướng nam thể thao tất cả các trận đấu giày da tăng chiều cao giày nam
}
英文:

Use func (*XMLElement) ChildText instead.

package main

import (
	"fmt"

	"github.com/gocolly/colly/v2"
)

func main() {
	c := colly.NewCollector()
	c.OnError(func(_ *colly.Response, err error) {
		fmt.Println("Something went wrong:", err)
	})
	c.OnXML("/html/body", func(e *colly.XMLElement) {
		child := e.ChildText("div[4]/div/div[3]/div[2]/div/div[1]/div[3]/div/div/h1")
		fmt.Println(child)
	})
	c.Visit("https://www.lazada.vn/-i1701980654-s7563711492.html")
	// Output:
	// Yierku 【Free Shipping Miễn phí vận chuyển】Giày nam mùa thu và mùa đông giày thường xu hướng nam thể thao tất cả các trận đấu giày da tăng chiều cao giày nam
}

huangapple
  • 本文由 发表于 2023年1月10日 12:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75065470.html
匿名

发表评论

匿名网友

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

确定