我正在尝试从电子商务网站上抓取顶级产品的名称列表。然而结果是空的。想知道缺少什么。输出为: 访问: https ://www.amazon.in/gp/bestsellers/electronics/ref=zg_bs_nav_0/ 抓取结束: https ://www.amazon.in/gp/bestsellers/electronics/ref=zg_bs_nav_0/
代码:
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"github.com/gocolly/colly"
)
func main() {
fetchURL := "https://www.amazon.in/gp/bestsellers/electronics/ref=zg_bs_nav_0/"
fileName := "results.csv"
file, err := os.Create(fileName)
if err != nil {
log.Fatal("ERROR: Could not create file %q: %s\n", fileName, err)
return
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
writer.Write([]string{"Sl. No."})
c := colly.NewCollector()
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting: ", r.URL)
})
c.OnHTML(`.a-section a-spacing-none aok-relative`, func(e *colly.HTMLElement) {
number := e.ChildText(".zg-badge-text")
name := e.ChildText(".p13n-sc-truncated")
writer.Write([]string{
number,
name,
})
c.Visit(fetchURL)
fmt.Println("End of scraping: ", fetchURL)
}
慕哥6287543
相关分类