-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraping.go
230 lines (176 loc) · 5.25 KB
/
scraping.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
package main
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/gocolly/colly"
)
// GetGroupURLs scrapes the main TBS collective agreements page and returns specific agreement URLs.
func GetGroupURLs(url string) []string {
// Initialize Colly Collector
c := colly.NewCollector(
colly.AllowedDomains("www.tbs-sct.gc.ca"),
)
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.OnError(func(_ *colly.Response, err error) {
log.Println("Something went wrong", err)
})
c.OnResponse(func(r *colly.Response) {
fmt.Println("Visited", r.Request.URL)
})
// set URLs for scraping
path := url
// set empty array for urls
urls := []string{}
// Test scraping function rates of pay
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
fmt.Println(link)
if strings.Contains(link, "rates") {
urls = append(urls, link)
}
})
c.OnScraped(func(r *colly.Response) {
fmt.Println("Finished", r.Request.URL)
})
c.Visit(path)
return urls
}
// GetPayScales parses a specific TBS Website looking for pay tables.
func GetPayScales(groupURL string, g *Group) {
// Initialize Colly Collector
c := colly.NewCollector(
colly.AllowedDomains("www.tbs-sct.gc.ca"),
)
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.OnError(func(_ *colly.Response, err error) {
log.Println("Something went wrong", err)
})
c.OnResponse(func(r *colly.Response) {
fmt.Println("Visited", r.Request.URL)
})
// set URLs for scraping
path := groupURL
// Test scraping function rates of pay
c.OnHTML("body", func(e *colly.HTMLElement) {
goquerySelection := e.DOM
g.Name = strings.TrimSpace(goquerySelection.Find("h1").Text())
g.PayScales = []PayScale{}
goquerySelection.Find("table").Each(func(index int, tablehtml *goquery.Selection) {
if index == 0 {
} else {
fmt.Println("Found Pay Table", index)
processTable(tablehtml, g)
}
})
})
c.OnScraped(func(r *colly.Response) {
fmt.Println("Finished", r.Request.URL)
})
c.Visit(path)
g.URL = groupURL
}
func processTable(tableObject *goquery.Selection, g *Group) {
fmt.Println("Processing table and generating payscale")
tableObject.Each(func(i int, table *goquery.Selection) {
rawCaption := strings.TrimSpace(table.Find("caption").Text())
// different groups format their captions differently.
// figure out which separator they use ":" or " - " and split on that
captionArray := []string{}
caption2 := ""
if strings.Contains(rawCaption, ":") {
// caption is split by :
captionArray = strings.Split(rawCaption, ":")
} else if strings.Contains(rawCaption, " - ") {
// caption is split by " - "
captionArray = strings.Split(rawCaption, " - ")
} else {
// caption isn't split
captionArray = append(captionArray, rawCaption)
}
if len(captionArray) > 1 {
caption2 = captionArray[1]
}
// extract level from name
level := 0
levelString := strings.Split(captionArray[0], "-")
if len(levelString) > 1 {
level, _ = strconv.Atoi(levelString[1])
} else {
level = 0
}
// Isn't empty
if captionArray[0] != "" &&
// Is under 12 characters
len(captionArray[0]) <= 12 &&
// is at least 3 characters
len(captionArray[0]) > 2 &&
// refers to annual pay
(strings.Contains(strings.ToLower(caption2), "annual") ||
caption2 == "") &&
// contains the identifer we are looking for
strings.Contains(strings.ToUpper(captionArray[0]), g.Identifier) &&
// not already duplicated in Payscales
!g.existsInPayScaleNames(captionArray[0]) &&
// able to extract numerical level from caption
level != 0 {
// Create PayScale struct
p := PayScale{
Name: captionArray[0],
Level: level,
}
// Find table body
tb := table.Find("tbody")
// Find each table row
tb.Find("tr").Each(func(rowIndex int, tr *goquery.Selection) {
// initialize row for salary
row := []int{}
date := "1980-01-01T11:45:26.371Z"
dateString := ""
// find the datetime value for the row
tr.Find("time").Each(func(indexOfTd int, th *goquery.Selection) {
dateString, _ = th.Attr("datetime")
date = dateString + "T11:45:26.371Z"
})
// Check if data is valid and, if so, scan rows
if date != "1980-01-01T11:45:26.371Z" {
// find each salary cell
tr.Find("td").Each(func(indexOfTd int, td *goquery.Selection) {
if strings.Contains(td.Text(), "to") {
payRange := strings.Split(td.Text(), " to ")
pay1, _ := strconv.Atoi(strings.TrimSpace(payRange[0]))
pay2, _ := strconv.Atoi(strings.TrimSpace(payRange[1]))
row = append(row, pay1, pay2)
} else {
pay := strings.Replace(td.Text(), ",", "", -1)
payAsNum, err := strconv.Atoi(pay)
if err != nil {
payAsNum = 0
}
row = append(row, payAsNum)
}
})
// if row totals zero or has no steps, disregard
}
// Check that there are non 0 values here
if len(row) > 0 &&
sum(row) > 0 {
inc := RateOfPay{
DateTime: dateString,
Salary: row,
}
p.RatesOfPay = append(p.RatesOfPay, inc)
p.Steps = len(inc.Salary)
}
})
// table row iterator complete, add payscale to group payscales
g.PayScales = append(g.PayScales, p)
}
})
}