diff options
| author | mo khan <mo@mokhan.ca> | 2022-04-17 20:23:52 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2022-04-17 20:23:52 -0600 |
| commit | 83dcd54b70df5deeb151ee6b49fb3a75a3bac62c (patch) | |
| tree | 96e13f73e59715db22667c6dd4b285047c27dfd4 | |
initial commit
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | main.go | 44 |
3 files changed, 51 insertions, 0 deletions
@@ -0,0 +1,5 @@ +module github.com/xlgmokha/tick + +go 1.18 + +require golang.org/x/net v0.0.0-20220412020605-290c469a71a5 @@ -0,0 +1,2 @@ +golang.org/x/net v0.0.0-20220412020605-290c469a71a5 h1:bRb386wvrE+oBNdF1d/Xh9mQrfQ4ecYhW5qJ5GvTGT4= +golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "net/http" + "os" + "strings" + + "golang.org/x/net/html" +) + +func MapFrom(attrs []html.Attribute) map[string]string { + m := map[string]string{} + for _, attr := range attrs { + m[attr.Key] = attr.Val + } + return m +} + +func Visit(n *html.Node) { + if n.Type == html.ElementNode && n.Data == "fin-streamer" { + m := MapFrom(n.Attr) + if m["data-field"] != "" && strings.Contains(m["data-field"], "Price") { + fmt.Printf("%v: %s\n", m["data-symbol"], m["value"]) + } + } + + for c := n.FirstChild; c != nil; c = c.NextSibling { + Visit(c) + } +} + +func main() { + response, err1 := http.Get("https://finance.yahoo.com/quote/MSFT/") + if err1 != nil { + os.Exit(1) + } + + doc, err := html.Parse(response.Body) + if err != nil { + os.Exit(2) + } + Visit(doc) +} |
