我试图刮掉产品的productId,但我不能。请帮助
html代码
<span class="info">
<button data-product="{"merchantName":"xxx","price":"11","productName":"car window","categoryName":"windows","brandName":"aa assosiations","productId":"which I want to scrape"}">
当我尝试
h.ChildAttr("span.info>button", "data-product")
结果是{"merchantName":"xxx","price":"11","productName":"car window","categoryName":"windows","brandName":"aa assosiations","productId":"which I want to scrape"}
当我尝试
h.ChildAttr("span.info>button", "productId")
没有结果。我怎样才能从colly那里得到这些数据?
属性值是一个原始值,在本例中,它是JSON格式的,因此您需要解析JSON以便正确获取数据。
例如:
package main
import (
"log"
"encoding/json"
"github.com/gocolly/colly"
)
func main() {
c := colly.NewCollector()
c.OnHTML(`body`, func(e *colly.HTMLElement) {
text := e.ChildAttr("span.info>button", "data-product")
var result map[string]interface{}
err := json.Unmarshal([]byte(text), &result)
if err != nil {
log.Println(err)
return
}
log.Println(result["productId"])
})
c.Visit("[some url]")
}
输出2021/10/21 14:23:24 which I want to scrape