-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory_urls.go
185 lines (153 loc) · 4.1 KB
/
category_urls.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
package goscrapeebay
import (
"bufio"
"net/url"
"strings"
"github.com/gocolly/colly/v2"
"github.com/gocolly/colly/v2/extensions"
)
func (s *EbayScraper) scrapeLeafCateories(rootcategoryURL *url.URL, writer *bufio.Writer) error {
s.logger.Info("Started scraping root category page", "url", rootcategoryURL.String())
errChan := make(chan error, 1)
c := colly.NewCollector(
colly.CacheDir(s.config.CacheDir),
)
c.Limit(&colly.LimitRule{
DomainGlob: "*.ebay.com",
Delay: s.config.Delay,
RandomDelay: s.config.RandomDelay,
})
extensions.RandomUserAgent(c)
c.OnRequest(func(r *colly.Request) {
s.logger.Info("Visiting", "url", r.URL.String())
})
c.OnResponse(func(r *colly.Response) {
s.logger.Info("Visited", "url", r.Request.URL.String())
})
c.OnError(func(r *colly.Response, err error) {
s.logger.Error("Requesting Err",
"url", r.Request.URL.String(),
"statusCode", r.StatusCode,
"err", err,
)
if r.StatusCode == 0 {
select {
case errChan <- ErrContextTimeout:
default:
// no op
}
}
})
c.OnHTML("body", func(h *colly.HTMLElement) {
s.collectLeafCategoryURLs(c, h, writer)
})
c.OnScraped(func(r *colly.Response) {
s.logger.Info("Scraped", "url", r.Request.URL.String())
})
go func() {
err := c.Visit(rootcategoryURL.String())
s.logger.Error("Visiting Err", "err", err)
if err != nil {
select {
case errChan <- err:
default:
// no op
}
}
close(errChan)
}()
err := <-errChan
return err
}
func (s *EbayScraper) collectLeafCategoryURLs(
c *colly.Collector,
h *colly.HTMLElement,
writer *bufio.Writer,
) {
url := h.Request.URL.String()
// check if the page is leaf category page
if s.isLeafCategoryPage(h) {
_, err := writer.WriteString(url + "\n")
if err != nil {
s.logger.Error("Failed to write to file", "err", err)
return
}
} else {
var count int
h.ForEach("div.dialog__cell > section:first-of-type", func(i int, m *colly.HTMLElement) {
title := m.ChildText("h2.section-title__title")
if title == "" {
s.logger.Warn("Carousel title empty", "url", url)
return
}
if title == "Shop by Category" {
m.ForEach("a.b-textlink", func(i int, d *colly.HTMLElement) {
if !strings.Contains(d.Text, "See all") {
if count > s.config.MaxCategoriesPerPage {
return
}
url := d.Attr("href")
err := c.Visit(url)
if err != nil {
s.logger.Error("Visiting Err", "err", err)
}
count++
}
})
}
})
count = 0
h.ForEach("section.brw-category-nav.brw-has-parentnode:first-of-type", func(i int, m *colly.HTMLElement) {
title := m.ChildText("span.textual-display.brw-category-nav__title")
if title == "" {
s.logger.Warn("Carousel title empty", "url", url)
return
}
if title == "Shop by Category" {
m.ForEach("a.textual-display.brw-category-nav__link", func(i int, d *colly.HTMLElement) {
if count > s.config.MaxCategoriesPerPage {
return
}
url := d.Attr("href")
err := c.Visit(url)
if err != nil {
s.logger.Error("Visiting Err", "err", err)
}
count++
})
}
})
}
}
func (s *EbayScraper) isLeafCategoryPage(h *colly.HTMLElement) bool {
isLeaf := false
url := h.Request.URL.String()
checkTitle := func(i int, g *colly.HTMLElement) {
title := g.ChildText("h2.section-title__title")
if title == "" {
s.logger.Warn("Carousel title empty", "url", url)
return
}
if title != "Shop by Category" && !isRootPage(s.config.TargetURL, url) {
isLeaf = true
}
}
selectors := []string{
"section.b-module.b-carousel.b-guidance.b-display--landscape:first-of-type",
"section.b-module.b-carousel.b-guidance--text.b-display--landscape:first-of-type",
"section.seo-guidance.seo-guidance__guidance_module:first-of-type",
"section.b-module.b-visualnav:first-of-type",
"section.brw-product-carousel:first-of-type",
}
for _, selector := range selectors {
h.ForEach(selector, checkTitle)
}
s.logger.Debug("Page type detected", "is_leaf", isLeaf, "url", url)
return isLeaf
}
func isRootPage(root string, u string) bool {
if root == u {
return true
}
return false
}