Skip to content

Instantly share code, notes, and snippets.

@obutora
Last active May 27, 2024 07:52
Show Gist options
  • Save obutora/da3ef5d943e782f5f2c398fea6117875 to your computer and use it in GitHub Desktop.
Save obutora/da3ef5d943e782f5f2c398fea6117875 to your computer and use it in GitHub Desktop.
Collyを使ったCrawler
package main
import (
"fmt"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
"github.com/gocolly/colly/v2"
)
func main() {
url := "https://www.amazon.co.jp/gp/bestsellers/fashion/"
// url := "https://www.amazon.co.jp/gp/bestsellers/fashion/?pg=2"
c := colly.NewCollector()
// Find and visit all links
c.OnHTML(".a-container", func(e *colly.HTMLElement) {
var (
name string
imgUrl string
)
e.ForEach("#gridItemRoot", func(i int, h *colly.HTMLElement) {
// urlString = h.Attr("href")
// title = strings.ReplaceAll(h.Text, "\n", "")
// imgUrl :=
h.ForEach("img", func(i int, h *colly.HTMLElement) {
imgUrl = h.Attr("src")
name = h.Attr("alt")
})
fmt.Printf("name: %v, imgUrl: %v\n", name, imgUrl)
})
})
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.Visit(url)
c.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment