-
Notifications
You must be signed in to change notification settings - Fork 2
/
huaaoxt.go
176 lines (140 loc) · 3.97 KB
/
huaaoxt.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package pholcus_lib
import (
// 基础包
"github.com/henrylee2cn/pholcus/app/downloader/request" //必需
"github.com/henrylee2cn/pholcus/common/goquery" //DOM解析
// "github.com/henrylee2cn/pholcus/logs" //信息输出
. "github.com/henrylee2cn/pholcus/app/spider" //必需
// . "github.com/henrylee2cn/pholcus/app/spider/common" //选用
// net包
// "net/http" //设置http.Header
// "net/url"
// 编码包
// "encoding/xml"
// "encoding/json"
// 字符串处理包
// "regexp"
"strconv"
// "strings"
// 其他包
//"fmt"
// "math"
// "time"
//"log"
//"log"
"fmt"
"strings"
)
func init() {
Huaaoxt.Register()
}
var Huaaoxt = &Spider{
Name: "华奥信托",
Description: "华奥信托净值数据 [Auto Page] [http://www.huaao-trust.com/list/705/1.shtml]",
// Pausetime: 300,
// Keyin: KEYIN,
// Limit: LIMIT,
NotDefaultField: true,
Namespace: func(*Spider) string {
return "xintuo"
},
// 子命名空间相对于表名,可依赖具体数据内容,可选
SubNamespace: func(self *Spider, dataCell map[string]interface{}) string {
return "fund_src_nav"
},
EnableCookie: false,
RuleTree: &RuleTree{
Root: func(ctx *Context) {
Keys := ctx.GetKeyin()
fmt.Println(Keys)
webpage := 42
var configs []string
configs = strings.Split(Keys, ",") //各种配置按照key1=value1,key2=value2,...的形式解析
for a := 0; a < len(configs); a++ {
if strings.Contains(configs[a], "page=") {
webpage, _ = strconv.Atoi(strings.TrimLeft(Keys, "page="))
fmt.Println(webpage)
}
}
ctx.Aid(map[string]interface{}{"loop": [2]int{1, webpage}, "Rule": "生成请求"}, "生成请求")
},
Trunk: map[string]*Rule{
"生成请求": {
AidFunc: func(ctx *Context, aid map[string]interface{}) interface{} {
page := 0
for loop := aid["loop"].([2]int); loop[0] < loop[1]; loop[0]++ {
page++
ctx.AddQueue(&request.Request{
Url: "http://www.huaao-trust.com/list/705/" + strconv.Itoa(loop[0]) + ".shtml",
Rule: aid["Rule"].(string),
Temp: map[string]interface{}{
"level1pages": page,
},
})
}
return nil
},
ParseFunc: func(ctx *Context) {
query := ctx.GetDom()
ss := query.Find(".news_list ul").Children()
var page1 int
ctx.GetTemp("level1pages", &page1)
page2 := 0
ss.Each(func(i int, goq *goquery.Selection) {
url, exist := goq.Find("a").Attr("href")
if exist {
page2++
newUrl := strings.Replace(url, "-", "/", 2)
newUrl1 := strings.Replace(newUrl, "_", "/", 1)
ctx.AddQueue(&request.Request{
Url: "http://www.huaao-trust.com" + newUrl1,
Rule: "获取结果",
Temp: map[string]interface{}{
"level1pages": page1,
"level2pages": page2,
},
})
}
})
},
},
"获取结果": {
//注意:有无字段语义和是否输出数据必须保持一致
ItemFields: []string{
"基金ID",
"名称",
"净值",
"累计净值",
"估值日期",
},
ParseFunc: func(ctx *Context) {
query := ctx.GetDom()
ss := query.Find(".news_details tbody").Find("tr")
count := 0
var page int
page = ctx.GetTemp("level1pages", &page).(int)
var page2 int
page2 = ctx.GetTemp("level2pages", &page2).(int)
ss.Each(func(i int, goq *goquery.Selection) {
titleLine := goq.Children().Eq(0).Text()
if titleLine != "产品名称" {
mingchen := goq.Children().Eq(0).Text()
jingzhi := goq.Children().Eq(3).Text()
leijijingzhi := goq.Children().Eq(3).Text()
guzhiriqi := goq.Children().Eq(2).Text()
count++
fundID := "XTHUAAO" + "P1" + strconv.Itoa(page) + "P2" + strconv.Itoa(page2) + "L" + strconv.Itoa(count)
ctx.Output(map[int]interface{}{
0: fundID,
1: mingchen,
2: jingzhi,
3: leijijingzhi,
4: guzhiriqi,
})
}
})
},
},
},
},
}