This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
271 lines (248 loc) · 6.76 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"fmt"
"github.com/antchfx/htmlquery"
"github.com/itning/DouBanReptile/internal/gui"
"github.com/itning/DouBanReptile/internal/ini"
"github.com/itning/DouBanReptile/internal/log"
"github.com/itning/DouBanReptile/internal/markdown"
"github.com/itning/DouBanReptile/internal/preference"
"github.com/itning/DouBanReptile/internal/request"
"github.com/itning/DouBanReptile/internal/scheduler"
"github.com/itning/DouBanReptile/internal/xpath"
"golang.org/x/net/html"
"os"
"regexp"
"strconv"
"strings"
"time"
)
var dispatcher scheduler.Dispatcher
var file *os.File
var priceCompile = regexp.MustCompile(`\b\d{4}\b`) // 匹配标题中价格正则
var dataArray = make(markdown.DataArray, 0)
var pre preference.Preference
func main() {
gui.Open(func(p preference.Preference) {
pre = p
savePreference(&p)
headerMap := make(map[string]string)
headerMap["User-Agent"] = request.UserAgentPCChrome
headerMap["Host"] = "www.douban.com"
dispatcher = scheduler.Dispatcher{
BaseUrl: "https://www.douban.com",
Headers: headerMap,
Cookies: request.AnalysisCookieString(p.CookieString),
}
dispatcher.Init2(
pre.GroupEntityURL,
`//td[@class='title']/a`,
each,
time.Millisecond*500,
&scheduler.PaginationRange{StartSize: 0, EndSize: p.MaxPage * 25, EveryAdd: 25})
write2File()
})
}
func savePreference(preference *preference.Preference) {
if preference.SavePreference {
config := ini.Config{}
config.Write(preference)
}
}
func write2File() {
var err error
file, err = os.Create("爬取结果.md")
if err != nil {
panic(err)
}
defer func() {
if err := file.Close(); err != nil {
panic(err)
}
}()
logger := log.GetImpl()
logger.Printf("共爬取条数: %d", len(dataArray))
logger.Printf("写入文件中....")
write([]byte(dataArray.String()))
logger.Printf("文件写入成功,请在本EXE目录下找【%s】文件", file.Name())
}
func write(b []byte) {
if _, err := file.Write(b); err != nil {
panic(err)
}
}
func each(nodes xpath.Nodes, request request.Data) {
if nil == nodes {
log.GetImpl().Printf("Node Is Nil. Url: %s", request.Url)
return
}
hrefs := nodes.Attr("href")
titles := nodes.Attr("title")
for index, href := range hrefs {
if isExcludeContent(titles[index]) {
continue
}
price := getPriceFromString(titles[index])
if pre.IncludeNoContentPriceCheck {
dispatcher.Add(href, `//div[@class="article"]`, content)
} else if 0 != price && price <= (pre.MaxPrice) {
// 标题上有价格并且价格在用户设置范围内
dispatcher.Add(href, `//div[@class="article"]`, content)
} else if 0 == price {
// 标题上没有价格但是内容中可能有价格
dispatcher.Add(href, `//div[@class="article"]`, contentWithTitleNoPrice)
}
}
}
// 只要有一个关键字存在即返回真
func isIncludeContent(content string) bool {
// 未设置关键字,则返回真
if 0 == len(pre.IncludeKeyArray) {
return true
}
for _, key := range pre.IncludeKeyArray {
if strings.Contains(content, key) {
return true
}
}
return false
}
// 只要关键字在标题或内容中即返回真
func checkTitleOrContentHaveKey(title string, content string) bool {
if isIncludeContent(title) {
return true
} else if isIncludeContent(content) {
return true
} else {
return false
}
}
func isExcludeContent(content string) bool {
for _, key := range pre.ExcludeKeyArray {
if strings.Contains(content, key) {
return true
}
}
return false
}
func getPriceFromString(title string) int {
priceArray := priceCompile.FindAllString(title, -1)
if len(priceArray) != 0 {
price, e := strconv.Atoi(priceArray[0])
if e != nil {
log.GetImpl().Printf("Transform Error %s", e.Error())
panic(e)
}
return price
} else {
return 0
}
}
func content(nodes xpath.Nodes, request request.Data) {
for _, node := range nodes {
// 处理标题
title := handleTitle(node)
// 处理内容
content := handleContent(node, request)
// 排除关键字
if isExcludeContent(content) {
continue
}
if !checkTitleOrContentHaveKey(title, content) {
continue
}
// 处理图片
imgArray := handleImages(node)
// 处理时间
timeStr := handleTime(node)
dataArray.Append(markdown.Data{
TimeString: timeStr,
Time: markdown.String2Time(timeStr),
Title: format(title),
Price: getPriceFromString(title),
Link: request.Url,
Content: content,
Images: imgArray,
})
}
}
func contentWithTitleNoPrice(nodes xpath.Nodes, request request.Data) {
for _, node := range nodes {
// 处理内容
content := handleContent(node, request)
price := getPriceFromString(content)
if 0 == price || price > (pre.MaxPrice) {
// 内容中价格依然没有或者价格大于用户设定价格
continue
}
// 排除关键字
if isExcludeContent(content) {
continue
}
// 处理标题
title := handleTitle(node)
if !checkTitleOrContentHaveKey(title, content) {
continue
}
// 处理图片
imgArray := handleImages(node)
// 处理时间
timeStr := handleTime(node)
dataArray.Append(markdown.Data{
TimeString: timeStr,
Time: markdown.String2Time(timeStr),
Title: format(title),
Price: getPriceFromString(title),
Link: request.Url,
Content: content,
Images: imgArray,
})
}
}
func handleTime(node *html.Node) string {
timeNode := htmlquery.FindOne(node, `//span[contains(@class,"color-green")]`)
timeStr := htmlquery.InnerText(timeNode)
return timeStr
}
func handleImages(node *html.Node) []string {
imgs := htmlquery.Find(node, `//div[@class="image-wrapper"]//img`)
imgArray := make([]string, 0)
if len(imgs) != 0 {
for _, img := range imgs {
imgArray = append(imgArray, htmlquery.SelectAttr(img, "src"))
}
}
return imgArray
}
func handleContent(node *html.Node, request request.Data) string {
contentNode := htmlquery.FindOne(node, `//td[@class="topic-content"]`)
if nil == contentNode {
contentNode = htmlquery.FindOne(node, `//div[@class="topic-richtext"]`)
}
if nil == contentNode {
contentNode = htmlquery.FindOne(node, `//div[@class="topic-content"]`)
}
if nil == contentNode {
contentNode = htmlquery.FindOne(node, `//div[@class='rich-content topic-richtext']`)
}
if nil == contentNode {
log.GetImpl().Printf("Content Is Nil So Jump Over. URL=%s", request.Url)
return fmt.Sprintf("<内容爬取失败 URL=%s>", request.Url)
}
content := htmlquery.InnerText(contentNode)
return content
}
func handleTitle(node *html.Node) string {
titleNode := htmlquery.FindOne(node, `//td[@class="tablecc"]`)
if nil == titleNode {
titleNode = htmlquery.FindOne(node, `//h1`)
}
title := htmlquery.InnerText(titleNode)
if strings.Contains(title, "标题") {
title = string([]rune(title)[3:])
}
return title
}
func format(str string) string {
return strings.TrimSpace(str)
}