/
acqNode.go
172 lines (134 loc) · 3.54 KB
/
acqNode.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
// Copyright 2014 mint.zhao.chiu@gmail.com. All rights reserved.
// Use of this source code is governed by a Apache License 2.0
// that can be found in the LICENSE file.
package goAcq
import (
"regexp"
"strconv"
"github.com/PuerkitoBio/goquery"
"strings"
)
type ETargetEncodeType string
const (
EncodeType_GB2312 ETargetEncodeType = "gb2312"
EncodeType_UTF8 ETargetEncodeType = "utf8"
EncodeType_BIG5 ETargetEncodeType = "big5"
)
type EMatchMode int32
const (
Mode_Regex EMatchMode = iota
Mode_String
)
var UrlPattern = "((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?"
type targetUrlMatch struct {
beginHtml string
endHtml string
imgHandleFlag bool
mustRegex string
noRegex string
}
type AcqNode struct {
nodeName string
targetEncode ETargetEncodeType
matchMode EMatchMode
targetListUrls []string
targetUrlRule *targetUrlMatch
targetRule []*AcqTarget
}
func NewDefaultAcqNode(nodeName string) *AcqNode {
return &AcqNode{
nodeName: nodeName,
targetEncode: EncodeType_GB2312,
matchMode: Mode_String,
targetListUrls: make([]string, 0),
targetUrlRule: &targetUrlMatch{
beginHtml: "",
endHtml: "",
imgHandleFlag: true,
mustRegex: "",
noRegex: "",
},
}
}
func (a *AcqNode) SetNodeName(nodeName string) {
a.nodeName = nodeName
}
func (a *AcqNode) GetNodeName() string {
return a.nodeName
}
func (a *AcqNode) SetTargetEncode(encode ETargetEncodeType) {
a.targetEncode = encode
}
func (a *AcqNode) GetTargetEncode() ETargetEncodeType {
return a.targetEncode
}
func (a *AcqNode) SetMatchMode(mode EMatchMode) {
a.matchMode = mode
}
func (a *AcqNode) GetMatchMode() EMatchMode {
return a.matchMode
}
func (a *AcqNode) SetTargetUrlBeginHtml(html string) {
a.targetUrlRule.beginHtml = html
}
func (a *AcqNode) GetTargetUrlBeginHtml() string {
return a.targetUrlRule.beginHtml
}
func (a *AcqNode) SetTargetUrlEndHtml(html string) {
a.targetUrlRule.endHtml = html
}
func (a *AcqNode) GetTargetUrlEndHtml() string {
return a.targetUrlRule.endHtml
}
func (a *AcqNode) addListUrl(url string) {
a.targetListUrls = append(a.targetListUrls, url)
}
// 添加采集列表
func (a *AcqNode) AddListUrls(urls ...string) (in []string) {
in = make([]string, 0)
for _, url := range urls {
if matched, err := regexp.MatchString(UrlPattern, url); matched && err == nil {
a.addListUrl(url)
in = append(in, url)
}
}
return
}
// http://www.aiwutech.com/test/list_(*).html
func (a *AcqNode) AddListUrlsByTag(urlMatch string, min, max, gap uint32) (in []string) {
pattern := "(*)"
if !strings.Contains(urlMatch, pattern) || (min > max) {
return
}
in = make([]string, 0)
for min <= max {
tmpNum := strconv.Itoa(int(min))
url := strings.Replace(urlMatch, pattern, tmpNum, -1)
in = append(in, url)
min += gap
}
return a.AddListUrls(in...)
}
// 采集目标列表个数
func (a *AcqNode) Len() int {
return len(a.targetListUrls)
}
// 采集列表url
func (a *AcqNode) GetTargetListUrls() []string {
return a.targetListUrls
}
func (a *AcqNode) Exec() {
for _, listUrl := range a.GetTargetListUrls() {
pageSource, err := readWebPageSource(listUrl, a.GetTargetEncode())
if err != nil {
continue
}
switch a.GetMatchMode() {
case Mode_Regex:
case Mode_String:
pageSource = strings.Split(pageSource, a.GetTargetUrlBeginHtml())[1]
pageSource = strings.Split(pageSource, a.GetTargetUrlEndHtml())[0]
println(pageSource)
}
}
}