Skip to content

Instantly share code, notes, and snippets.

@mottet-dev
mottet-dev / main.go
Created June 15, 2019 16:02
CSV For each - Go Colly
writer.Write([]string{
productName,
stars,
price,
})
@mottet-dev
mottet-dev / main.go
Last active June 15, 2019 15:59
CSV Writer - Go Colly Advanced
func main() {
fileName := "amazon_products.csv"
file, err := os.Create(fileName)
if err != nil {
log.Fatalf("Could not create %s", fileName)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
@mottet-dev
mottet-dev / main.go
Created June 15, 2019 15:36
Proxy Switcher - Go Colly
proxySwitcher, err := proxy.RoundRobinProxySwitcher("socks5://188.226.141.127:1080", "socks5://67.205.132.241:1080")
if err != nil {
log.Fatal(err)
}
c.SetProxyFunc(proxySwitcher)
@mottet-dev
mottet-dev / main.go
Last active June 15, 2019 14:22
Ramdom Dellay - Go Colly Advanced
c.Limit(&colly.LimitRule{
RandomDelay: 2 * time.Second,
Parallelism: 4,
})
@mottet-dev
mottet-dev / main.go
Created June 11, 2019 20:42
Paralellism - Go Colly Advanced
func main() {
c := colly.NewCollector(
colly.Async(true),
)
// ...
c.Wait()
}
@mottet-dev
mottet-dev / main.go
Created June 11, 2019 20:31
Handle Pagination - Go Colly Advanced
for i := 1; i <= 20; i++ {
fullURL := fmt.Sprintf("https://www.amazon.com/s?k=nintendo+switch&page=%d", i)
c.Visit(fullURL)
}
@mottet-dev
mottet-dev / main.go
Created June 11, 2019 20:21
User-Agent - Go Colly Advanced
import (
"fmt"
"github.com/gocolly/colly"
"github.com/icrowley/fake"
"github.com/mottet-dev/medium-go-colly-basics/utils"
)
func main() {
c := colly.NewCollector()
@mottet-dev
mottet-dev / main.go
Last active June 10, 2019 17:19
Final (The Basics) - Go Colly
package main
import (
"fmt"
"github.com/gocolly/colly"
"github.com/mottet-dev/medium-go-colly-basics/utils"
)
func main() {
@mottet-dev
mottet-dev / utils.go
Created June 10, 2019 16:49
PriceFormat - Go Colly
package utils
import "regexp"
func FormatPrice(price *string) {
r := regexp.MustCompile(`\$(\d+(\.\d+)?).*$`)
newPrices := r.FindStringSubmatch(*price)
if len(newPrices) > 1 {
@mottet-dev
mottet-dev / main.go
Created June 10, 2019 16:27
ForEach (Name) - Go Colly
package main
import (
"fmt"
"github.com/gocolly/colly"
)
func main() {
c := colly.NewCollector()