1 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
您需要添加 User-Agent 標(biāo)頭才能返回?cái)?shù)據(jù)。它似乎p13n-sc-truncated也是一個(gè)生成的類名。您可以使用以下示例:
package main
import (
"log"
"strings"
"github.com/gocolly/colly"
)
type AmazonData struct {
Index int
Link string
Title string
}
func main() {
c := colly.NewCollector()
var data []AmazonData
count := 1
c.OnHTML(`#zg-ordered-list`, func(e *colly.HTMLElement) {
e.ForEach("li .zg-item", func(_ int, elem *colly.HTMLElement) {
link := elem.DOM.Find("a")
linkHref, _ := link.Attr("href")
data = append(data, AmazonData{
Index: count,
Link: linkHref,
Title: strings.TrimSpace(link.Find("div").Text()),
})
count++
})
log.Println(data)
})
c.OnRequest(func(r *colly.Request) {
r.Headers.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36")
})
c.Visit("https://www.amazon.in/gp/bestsellers/electronics/ref=zg_bs_nav_0/")
}
- 1 回答
- 0 關(guān)注
- 126 瀏覽
添加回答
舉報(bào)