Skip to content

Instantly share code, notes, and snippets.

@mottet-dev
Last active June 10, 2019 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mottet-dev/f74d6f278f098299a7ea65927283b04a to your computer and use it in GitHub Desktop.
Save mottet-dev/f74d6f278f098299a7ea65927283b04a to your computer and use it in GitHub Desktop.
Final (The Basics) - Go Colly
package main
import (
"fmt"
"github.com/gocolly/colly"
"github.com/mottet-dev/medium-go-colly-basics/utils"
)
func main() {
c := colly.NewCollector()
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.OnHTML("div.s-result-list.s-search-results.sg-row", func(e *colly.HTMLElement) {
e.ForEach("div.a-section.a-spacing-medium", func(_ int, e *colly.HTMLElement) {
var productName, stars, price string
productName = e.ChildText("span.a-size-medium.a-color-base.a-text-normal")
if productName == "" {
// If we can't get any name, we return and go directly to the next element
return
}
stars = e.ChildText("span.a-icon-alt")
utils.FormatStars(&stars)
price = e.ChildText("span.a-price > span.a-offscreen")
utils.FormatPrice(&price)
fmt.Printf("Product Name: %s \nStars: %s \nPrice: %s \n", productName, stars, price)
})
})
c.Visit("https://www.amazon.com/s?k=nintendo+switch&ref=nb_sb_noss_1")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment