-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapper.go
250 lines (209 loc) · 7.59 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"fmt"
"log"
"net/url"
"strings"
"github.com/gocolly/colly/v2"
)
type ProductAttribute string
type ProductAttributeValue string
type Category string
// ProductDataMap stores the product values of all required product attributes
type ProductDataMap map[ProductAttribute][]ProductAttributeValue
// CategoryProductsMap stores the product data per category
type CategoryProductsMap map[Category][]ProductDataMap
// Define multiple disallowed robots tags (modify as needed)
var disallowedTags []string = []string{"noindex", "nofollow"}
// findColIndex finds the index of a column by name in the header row
func findColIndex(header []string, name string) int {
for i, colName := range header {
if colName == name {
return i
}
}
return -1
}
func scrapeCategoryCatalogPage(categoryData CategorySchemaStruct) []ProductDataMap {
products := []ProductDataMap{}
// Instantiate default collector
catalogCollector := colly.NewCollector()
userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
catalogCollector.UserAgent = userAgent
catalogCollector.OnRequest(func(r *colly.Request) {
log.Println("Visiting product catalog page:", r.URL)
})
catalogCollector.OnError(func(r *colly.Response, err error) {
log.Println("Error scraping product catalog page:", err)
})
allowed := true
catalogCollector.OnResponse(func(r *colly.Response) {
log.Println("Received response from catalog page:", r.Request.URL)
robotsTag := r.Headers.Get("X-Robots-Tag")
if robotsTag != "" {
for _, disallowedTag := range disallowedTags {
if strings.Contains(strings.ToLower(robotsTag), disallowedTag) {
fmt.Println("Found disallowed robots tag:", robotsTag)
// Ignore the response and cancel visit
allowed = false
return
}
}
}
// Proceed with normal processing if no disallowed tag found
log.Println("Processing response from:", r.Request.URL)
})
catalogCollector.OnHTML(categoryData.ProductPageElement, func(h *colly.HTMLElement) {
if !allowed {
return
}
visitLink := h.Attr("href")
log.Println("Found link:", visitLink)
if !strings.HasPrefix(visitLink, "http") {
visitLink = h.Request.AbsoluteURL(visitLink)
if !strings.HasPrefix(visitLink, "http") {
// HTML may have the "base href" set to "/", so we need to account for that
visitLink = h.Request.URL.Scheme + "://" + h.Request.URL.Host + visitLink
}
}
products = append(products, scrapeProductLink(visitLink, categoryData.ProductElements))
})
catalogCollector.OnHTML(categoryData.NextProductPageElement, func(h *colly.HTMLElement) {
if !allowed {
return
}
visitLink := h.Attr("href")
log.Println("\nFound Next link:", visitLink)
if !strings.HasPrefix(visitLink, "http") {
visitLink = h.Request.AbsoluteURL(visitLink)
if !strings.HasPrefix(visitLink, "http") {
// HTML may have the "base href" set to "/", so we need to account for that
visitLink = h.Request.URL.Scheme + "://" + h.Request.URL.Host + visitLink
}
}
catalogCollector.Visit(visitLink)
})
catalogCollector.OnScraped(func(r *colly.Response) {
log.Println("Scraped product catalog page:", r.Request.URL)
})
catalogCollector.Visit(categoryData.URL)
return products
}
// Function to check if a URL is absolute
func isAbsoluteURL(urlString string) bool {
parsedUrl, err := url.Parse(urlString)
return err == nil && parsedUrl.Scheme != ""
}
// Function to clean and construct absolute URL
func cleanUrl(baseUrl *url.URL, relUrl string) (string, error) {
// Handle absolute URLs directly
if isAbsoluteURL(relUrl) {
return relUrl, nil
}
// Parse the relative URL
rel, err := url.Parse(relUrl)
if err != nil {
return "", err
}
// Use the url package's ResolveReference function to resolve the relative URL against the base URL
resolvedUrl := baseUrl.ResolveReference(rel)
return resolvedUrl.String(), nil
}
// removeQueryPart is a function to remove the query part from a urlString
func removeQueryPart(urlString string) (string, error) {
url, err := url.Parse(urlString)
if err != nil {
log.Println("Error encountered in parsing URL")
return "", err
}
url.RawQuery = ""
return url.String(), nil
}
func scrapeProductLink(productLink string, productElements ProductElementsSchemaMap) (product ProductDataMap) {
localProductElements := productElements
product = make(ProductDataMap)
// Create a collector for scraping product pages
collector := colly.NewCollector()
userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/000000000 Safari/537.36"
collector.UserAgent = userAgent
collector.OnRequest(func(r *colly.Request) {
log.Println("Visiting product page:", r.URL)
})
collector.OnError(func(r *colly.Response, err error) {
log.Println("Error scraping product page:", err)
})
allowed := true
// Check for "X-Robots-Tag" header in OnResponse
collector.OnResponse(func(r *colly.Response) {
log.Println("Received response from product page:", r.Request.URL)
robotsTag := r.Headers.Get("X-Robots-Tag")
if robotsTag != "" {
for _, disallowedTag := range disallowedTags {
if strings.Contains(strings.ToLower(robotsTag), disallowedTag) {
log.Println("Found disallowed robots tag:", robotsTag)
// Ignore the response and cancel visit
allowed = false
return
}
}
}
// Proceed with normal processing if no disallowed tag found
log.Println("Processing response from:", r.Request.URL)
})
for _, productElement := range localProductElements {
localProductElement := productElement
collector.OnHTML(localProductElement, func(h *colly.HTMLElement) {
if !allowed {
return
}
// log.Printf("Found product element: %s\n", productElement)
attribute, err := localProductElements.findKey(localProductElement)
localProductAttribute := ProductAttribute(attribute)
if err != nil {
log.Println("Error: Could not find key for product element:", productElement)
return
}
// if productElement represents an image then get the src attribute
// other wise get the text content
switch {
case strings.Contains(strings.ToLower(localProductElement), "img"):
if ProductAttributeValue(h.Attr("src")) != "" {
if !isAbsoluteURL(h.Attr("src")) {
// get base url
// Get base URL and handle non-absolute URLs
baseUrl, err := cleanUrl(h.Request.URL, h.Attr("src"))
if err != nil || baseUrl == "" {
log.Println("Error: Could not construct absolute URL")
return
}
baseUrl, err = removeQueryPart(baseUrl)
if err != nil {
log.Println("Error: Could not remove query part from URL")
return
}
product[localProductAttribute] = append(product[localProductAttribute], ProductAttributeValue(baseUrl))
} else {
baseUrl, err := removeQueryPart(h.Attr("src"))
if err != nil {
log.Println("Error: Could not remove query part from URL")
return
}
product[localProductAttribute] = append(product[localProductAttribute], ProductAttributeValue(baseUrl))
}
}
default:
// log.Printf("Found product element value: %s for product attribute: %s\n", h.Text, localProductAttribute)
if ProductAttributeValue(h.Text) != "" {
product[localProductAttribute] = append(product[localProductAttribute], ProductAttributeValue(h.Text))
}
}
// log.Println("Found product element:", productElement)
})
}
collector.OnScraped(func(r *colly.Response) {
log.Println("Scraped product page:", r.Request.URL)
product["URL"] = append(product["URL"], ProductAttributeValue(r.Request.URL.String()))
})
collector.Visit(productLink)
return
}