-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.go
More file actions
56 lines (41 loc) · 1.04 KB
/
scraper.go
File metadata and controls
56 lines (41 loc) · 1.04 KB
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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/gocolly/colly"
)
type book struct {
ID int `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
Price string `json:"price"`
}
func scrape() {
c := colly.NewCollector()
i := 1
var books []book
c.OnHTML("div.col-sm-20 div.card.align-items-center div.card-body.position-relative p.card-text.text-center", func(h *colly.HTMLElement) {
book := book{
ID: i,
Title: h.ChildText("span.booktitle"),
Author: h.ChildText("span.author"),
Price: h.ChildText("span.actualprice"),
}
i += 1
// fmt.Println(h.ChildText("span.booktitle"))
// fmt.Println(h.ChildText("span.author"))
// fmt.Println(h.ChildText("span.actualprice"))
books = append(books, book)
})
c.Visit("https://www.bookswagon.com/promo-best-seller/best-seller/03AC998EBDC2")
// fmt.Println(books)
createJSON(books)
}
func createJSON(b []book) {
content, err := json.Marshal(b)
if err != nil {
fmt.Println(err.Error())
}
os.WriteFile("books.json", content, 0644)
}