sunty
2021-06-03 72ccf8a395203671f8a1e80d04c7418544f315f9
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package controllers
 
import (
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/gocolly/colly"
    "log"
    "reptile/code"
    "reptile/util"
)
 
type ReptileControllers struct{}
 
type FieldSelectorInfo struct {
    Name     string `json:"name"`
    Selector string `json:"selector"`
}
 
type FieldResult struct {
    Name  string
    Value string
}
 
type ReptileInfo struct {
    Url              string              `json:"url"`
    UserName         string              `json:"userName"`
    UserPassWD       string              `json:"userPassWD"`
    TitleSelectorStr string              `json:"titleSelectorStr"`
    ListSelectorStr  string              `json:"listSelectorStr"`
    ListRequest      bool                `json:"listRequest"`
    FspArray         []FieldSelectorInfo `json:"fspArray"`
}
 
func (rc *ReptileControllers) Reptile(c *gin.Context) {
    var rpif ReptileInfo
    c.BindJSON(&rpif)
    url := rpif.Url
    titleSelectorStr := rpif.TitleSelectorStr
    listSelectorStr := rpif.ListSelectorStr
    fspArray := rpif.FspArray
    listRequest := rpif.ListRequest
    if listRequest == true {
        if url != "" && titleSelectorStr != "" && listSelectorStr != "" && fspArray != nil && len(fspArray) > 0 {
            res := ReptileList(url, titleSelectorStr, listSelectorStr, fspArray)
            util.ResponseFormat(c, code.Success, res)
            return
        } else {
            util.ResponseFormat(c, code.RequestParamError, "请求参数有误")
            return
        }
    } else {
        if url != "" && titleSelectorStr != "" && fspArray != nil && len(fspArray) > 0 {
            res := ReptileNoList(url, titleSelectorStr, fspArray)
            util.ResponseFormat(c, code.Success, res)
            return
        } else {
            util.ResponseFormat(c, code.RequestParamError, "请求参数有误")
            return
        }
    }
    return
}
 
func ReptileList(url string, titleSelectorStr string, listSelectorStr string, fspArray []FieldSelectorInfo) interface{} {
    cy := colly.NewCollector()
    rfr := make([]interface{}, 0)
    cy.OnHTML(titleSelectorStr, func(e *colly.HTMLElement) {
        fmt.Println("+++++++++++++++++++++++++++")
        e.ForEach(listSelectorStr, func(i int, ele *colly.HTMLElement) {
            fr := []FieldResult{}
            for _, v := range fspArray {
                fr = append(fr, FieldResult{
                    Name:  v.Name,
                    Value: ele.ChildText(v.Selector),
                })
            }
            rfr = append(rfr, fr)
        })
    })
    err := cy.Request("GET", url, nil, nil, nil)
    if err != nil {
        log.Fatal("connect error: ", err)
    }
    return rfr
}
 
func ReptileNoList(url string, titleSelectorStr string, fspArray []FieldSelectorInfo) []interface{} {
    c := colly.NewCollector()
    rfr := make([]interface{}, 0)
    titleSelectorStr = "body"
    c.OnHTML(titleSelectorStr, func(e *colly.HTMLElement) {
        fmt.Println("+++++++++++++++++++++++++++")
        fr := []FieldResult{}
        for _, v := range fspArray {
            fmt.Println("v.Selector: ", v.Selector)
            fr = append(fr, FieldResult{
                Name:  v.Name,
                Value: e.DOM.Find(v.Selector).Eq(0).Text(),
            })
        }
        rfr = append(rfr, fr)
    })
    err := c.Request("GET", url, nil, nil, nil)
    if err != nil {
        log.Fatal("connect error: ", err)
    }
    return rfr
}