-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoodquotes.go
62 lines (52 loc) · 1.59 KB
/
goodquotes.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
package goodquotes
import (
"strconv"
"strings"
"github.com/gocolly/colly/v2"
)
type Quote struct {
TextQuote string
Author string
Tags []string
Likes uint64
}
type ScrapedData struct {
Name string
Pages uint64
Quotes []Quote
}
func Quotes(query string, page int) ScrapedData {
scraper := colly.NewCollector()
var scrapedData ScrapedData
scraper.OnHTML(".mainContentContainer .mainContent .mainContentFloat", func(resp *colly.HTMLElement) {
scrapedData.Name = resp.ChildText("h1")
PageDiv := resp.ChildText("div.leftContainer div:nth-child(33) div")
if len(PageDiv) > 0 {
Pages := strings.Fields(PageDiv)
if len(Pages) > 0 {
Pages := Pages[len(Pages)-3]
scrapedData.Pages, _ = strconv.ParseUint(Pages, 10, 64)
} else {
scrapedData.Pages = 0
}
}
resp.ForEach(".leftContainer .quote", func(_ int, e *colly.HTMLElement) {
quoteText := e.ChildText("div.quoteText")
likes := e.ChildText("div.quoteFooter div.right a")
likeCount, _ := strconv.ParseUint(strings.Fields(likes)[0], 10, 64)
scrapedData.Quotes = append(scrapedData.Quotes, Quote{
TextQuote: strings.TrimSpace(strings.Split(quoteText, "―")[0]),
Author: strings.TrimSpace(strings.Split(quoteText, "―")[1]),
Tags: e.ChildTexts("div.quoteFooter .greyText a"),
Likes: likeCount,
})
})
})
query = strings.ReplaceAll(query, " ", "-")
if page > 1 {
scraper.Visit("https://www.goodreads.com/quotes/tag/" + query + "?page=" + strconv.Itoa(page))
} else {
scraper.Visit("https://www.goodreads.com/quotes/tag/" + query + "?page=1")
}
return scrapedData
}