-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (49 loc) · 1.42 KB
/
main.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
package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/gocolly/colly"
)
type Currency struct {
Title string `json:"title"`
PriceUsd string `json:"priceUsd"`
PriceBtc string `json:"PriceBtc"`
Sum string `json:"sum"`
}
var count int
var currencies = []Currency{}
func main() {
scrapPage("https://bitinfocharts.com/ru/spisok-luchshih-kriptovalyut.html")
println("Find ", count, " currencies")
writeResultToXls()
}
func scrapPage(url string) {
c := colly.NewCollector()
c.OnHTML("tr", func(e *colly.HTMLElement) {
if e.Attr("class") == "ptr" {
title := e.DOM.Find("td:nth-child(2)").Text()
priceUsd := e.DOM.Find("td:nth-child(3) a").Text()
priceBtc := e.DOM.Find("td:nth-child(4) span:nth-child(1)").Text()
sum := e.DOM.Find("td:nth-child(6) span:nth-child(1)").Text()
cur := Currency{title, priceUsd, priceBtc, sum}
count++
currencies = append(currencies, cur)
}
})
c.Visit(url)
}
func writeResultToXls() {
xlsx := excelize.NewFile()
xlsx.NewSheet("Sheet1")
for i, cur := range currencies {
xlsx.SetCellValue("Sheet1", fmt.Sprintf("A%v", i+1), cur.Title)
xlsx.SetCellValue("Sheet1", fmt.Sprintf("B%v", i+1), cur.PriceUsd)
xlsx.SetCellValue("Sheet1", fmt.Sprintf("C%v", i+1), cur.PriceBtc)
xlsx.SetCellValue("Sheet1", fmt.Sprintf("D%v", i+1), cur.Sum)
}
err := xlsx.SaveAs("./currencies.xlsx")
if err != nil {
fmt.Println(err)
}
println("Finish...")
}