-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapper.go
142 lines (102 loc) · 2.73 KB
/
scrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"encoding/csv"
"fmt"
"os"
"github.com/gocolly/colly/v2"
)
type PokemonProduct struct{
url , image,name , price string
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str{
return false
}
}
return false
}
var pokemonProducts []PokemonProduct
func main() {
// Instantiate default collector
c := colly.NewCollector()
i := 1
limit := 5
var pagesToScrape []string
pageToScrape := "https://scrapeme.live/shop/page/1"
// initializing the list of pages discovered with a pageToScrape
pagesDiscovered := []string{ pageToScrape }
c.OnHTML("a.page-numbers" , func( e *colly.HTMLElement){
newPaginationLink := e.Attr("href")
if !contains(pagesToScrape,newPaginationLink){
pagesToScrape = append(pagesToScrape,newPaginationLink)
}
pagesDiscovered = append(pagesDiscovered, newPaginationLink)
})
c.OnHTML( "li.product" , func(e *colly.HTMLElement){
// Extract data from the HTML elements
url := e.ChildAttr("a.woocommerce-LoopProduct-link", "href")
image := e.ChildAttr("img", "src")
name := e.ChildText("h2.woocommerce-loop-product__title")
price := e.ChildText("span.price")
// Append the extracted data to the slice
pokemonProducts = append(pokemonProducts, PokemonProduct{
url: url,
image: image,
name: name,
price: price,
})
})
c.OnScraped( func(r *colly.Response){
if len(pagesToScrape) != 0 && i < limit {
pageToScrape = pagesToScrape[0]
pagesToScrape = pagesToScrape[1:]
i++
c.Visit(pageToScrape)
}
})
c.OnRequest( func( r *colly.Request){
fmt.Println("Visiting : ",r.URL.String())
})
// setting a valid User-Agent header
c.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
// downloading the target HTML page
// visiting the first page
c.Visit(pageToScrape)
for _, pokemonProduct := range pokemonProducts {
// converting a PokemonProduct to an array of strings
fmt.Printf("%v , %v , %v , %v \n",
pokemonProduct.url,
pokemonProduct.image,
pokemonProduct.name,
pokemonProduct.price)
}
//openeing a CSV file
file , err := os.Create("pokemon.csv")
if err != nil{
panic(err)
}
defer file.Close()
writer := csv.NewWriter(file)
headers:= []string{
"url","image","name","price",
}
err = writer.Write(headers)
if err != nil{
panic(err)
}
for _, pokemonProduct := range pokemonProducts {
// converting a PokemonProduct to an array of strings
record := []string{
pokemonProduct.url,
pokemonProduct.image,
pokemonProduct.name,
pokemonProduct.price,
}
err = writer.Write(record)
if err != nil{
panic(err)
}
}
defer writer.Flush()
}