我嘗試刮取產品的 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 獲取這些資料?
uj5u.com熱心網友回復:
屬性值是一個原始值,在這種情況下,它是 JSON 格式,因此您需要決議 JSON 才能正確獲取資料。
例如:
package main
import (
"log"
"encoding/json"
"github.com/gocolly/colly"
)
type Data struct {
MerchantName string
CategoryName string
BrandName string
ProductId string
}
func main() {
c := colly.NewCollector()
var data Data
c.OnHTML(`body`, func(e *colly.HTMLElement) {
text := e.ChildAttr("span.info>button", "data-product")
err := json.Unmarshal([]byte(text), &data)
if err != nil {
log.Println(err)
return
}
log.Println(data.ProductId)
})
c.Visit("[some url]")
}
輸出
2021/10/21 14:23:24 which I want to scrape
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329259.html
