Created
January 3, 2019 04:52
-
-
Save novalagung/6df5cd92097c94a55f3e45b0358fd27a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"strconv" | |
"strings" | |
"github.com/PuerkitoBio/goquery" | |
) | |
type Kurs struct { | |
Name string | |
Currency string | |
Unit int64 | |
Sell float64 | |
Buy float64 | |
Avg float64 | |
} | |
func getKursBiAt(dateString string) ([]Kurs, error) { | |
url := fmt.Sprintf("https://www.ortax.org/ortax/?mod=kursbi&search=%s", dateString) | |
res, err := http.Get(url) | |
if err != nil { | |
return nil, err | |
} | |
defer res.Body.Close() | |
if res.StatusCode != 200 { | |
return nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status) | |
} | |
fmt.Println("asdfasdfasdf") | |
doc, err := goquery.NewDocumentFromReader(res.Body) | |
if err != nil { | |
return nil, err | |
} | |
kurs := make([]Kurs, 0) | |
doc.Find(".bs-docs-example").First().Find("tr").Each(func(i int, s *goquery.Selection) { | |
if s.Find("td").Length() == 0 { | |
return | |
} | |
k := Kurs{} | |
nameTemp, _ := s.Find("td").Eq(0).Html() | |
k.Name = strings.Split(nameTemp, " [")[0] | |
k.Currency = strings.Split(strings.Split(nameTemp, `">`)[1], `</`)[0] | |
unitTemp, _ := s.Find("td").Eq(1).Html() | |
k.Unit, _ = strconv.ParseInt(unitTemp, 10, 64) | |
sellTemp, _ := s.Find("td").Eq(2).Find("span").Html() | |
k.Sell, _ = strconv.ParseFloat(sellTemp, 64) | |
buyTemp, _ := s.Find("td").Eq(3).Find("span").Html() | |
k.Buy, _ = strconv.ParseFloat(buyTemp, 64) | |
avgTemp, _ := s.Find("td").Eq(4).Find("span").Html() | |
k.Avg, _ = strconv.ParseFloat(avgTemp, 64) | |
kurs = append(kurs, k) | |
}) | |
return kurs, nil | |
} | |
func main() { | |
res, err := getKursBiAt("2019-1-2") | |
if err != nil { | |
fmt.Println("err", err.Error()) | |
} | |
fmt.Printf("%#v \n", res) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment