-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic_scraper.go
74 lines (55 loc) · 1.43 KB
/
magic_scraper.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
package main
import (
"encoding/json";
"fmt";
"github.com/gocolly/colly";
)
const EDHREC_BASE_URL string = "https://edhrec.com/"
var EDHREC_URLS = []string{
"top/",
"top/week",
"top/month",
"/commanders",
"/commanders/week",
"/commanders/month",
}
type Card struct {
name string
set string
foil bool
avg_price float64
}
func main() {
c := colly.NewCollector()
c.OnHTML("script[type='application/json']", func(e *colly.HTMLElement) {
var jsonData map[string]interface{}
err := json.Unmarshal([]byte(e.Text), &jsonData)
if err != nil {
fmt.Println("Error parsing JSON")
return
}
cardlists := jsonData["props"].
(map[string]interface{})["pageProps"].
(map[string]interface{})["data"].
(map[string]interface{})["container"].
(map[string]interface{})["json_dict"].
(map[string]interface{})["cardlists"].
([]interface{})[0].
(map[string]interface{})["cardviews"].
([]interface{})
cards := make([]string, len(cardlists))
for _, card := range cardlists {
card := card.(map[string]interface{})
cards = append(cards, card["sanitized_wo"].(string))
}
fmt.Println(cards)
fmt.Println(len(cards))
})
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting : ", r.URL.String())
})
for _, url := range EDHREC_URLS {
full_url := EDHREC_BASE_URL + url
c.Visit(full_url)
}
}