/
process_html.go
65 lines (59 loc) · 1.5 KB
/
process_html.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
package crawl
import (
"fmt"
"io"
"net/http"
"github.com/PuerkitoBio/goquery"
"github.com/cnk3x/urlx"
)
// ProcessHtml Html选择器
func ProcessHtml(readHtml func(doc *goquery.Selection) error) urlx.Process {
return func(resp *http.Response, body io.ReadCloser) error {
defer body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("%s(%d)", resp.Status, resp.StatusCode)
}
doc, err := goquery.NewDocumentFromReader(body)
if err != nil {
return fmt.Errorf("read as html: %w", err)
}
return readHtml(doc.Selection)
}
}
// ProcessStruct Html解析到Struct
func ProcessStruct(rootSelect string, out any, options StructOptions) urlx.Process {
return ProcessHtml(func(doc *goquery.Selection) error {
return BindStruct(doc.Find(rootSelect), out, options)
})
}
// ProcessMap Html解析到Map, out must map[string]any or []map[string]any
func ProcessMap(out any, params map[string]string, options MapField) urlx.Process {
return ProcessHtml(func(doc *goquery.Selection) error {
data, err := BindMapField(doc, params, options)
if err != nil {
return err
}
if len(data) == 0 {
return nil
}
var ok bool
switch t := out.(type) {
case *any:
*t = data
ok = true
case *map[string]any:
*t, ok = data[0].(map[string]any)
case *[]map[string]any:
*t = make([]map[string]any, len(data))
for i, x := range data {
if (*t)[i], ok = x.(map[string]any); !ok {
break
}
}
}
if !ok {
return fmt.Errorf("can not set %T to %T", data, out)
}
return nil
})
}