Skip to content

Commit

Permalink
Merge pull request amir-the-h#18 from james-zhang-bing/main
Browse files Browse the repository at this point in the history
should return error when the code != 0
  • Loading branch information
amir-the-h authored Sep 17, 2022
2 parents eb59333 + 2c04f25 commit cc1ba18
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
53 changes: 53 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"log"
"regexp"

"github.com/PuerkitoBio/goquery"
)

func main() {

doc, err := goquery.NewDocument("https://www.okx.com/docs-v5/zh/")
if err != nil {
log.Fatal(err)
}
//> result
rex:=regexp.MustCompile(`\w+`)

m := make(map[string][]string)
table := doc.Find("table")
table.Each(func(i int, s *goquery.Selection) {
s.Find("tbody").Each(func(i int, s *goquery.Selection) {
s.Find("tr").Each(func(i int, s *goquery.Selection) {
trs := make([]string, 0)
s.Find("td").Each(func(i int, s *goquery.Selection) {
trs = append(trs, s.Text())
})
if len(trs) == 0 {
return
}

key:=rex.FindString(trs[0])
if key==""{
return
}
//fmt.Println(key)
if _, ok := m[key]; !ok {
m[key] = trs
}

})
})
})
//num := 0
for i, v := range m {
fmt.Printf("%s ----> %v\n", i, v)
// num++
// if num == 10 {
// break
// }
}
}
20 changes: 20 additions & 0 deletions responses/responses.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
package responses

import (
"encoding/json"
"fmt"
)

type (
Basic struct {
Code int `json:"code,string"`
Msg string `json:"msg,omitempty"`
}
)

type resCode Basic

func (b *Basic) UnmarshalJSON(bf []byte) error {
var r resCode
err := json.Unmarshal(bf, &r)
if err != nil {
return err
}
b = (*Basic)(&r)
if b.Code != 0 {
return fmt.Errorf("recevied error:%+v", b)
}
return nil
}

0 comments on commit cc1ba18

Please sign in to comment.