/
spider_zhihu.go
370 lines (325 loc) · 6.62 KB
/
spider_zhihu.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package main
import (
"bufio"
"fmt"
"github.com/polaris1119/bitmap"
"golang.org/x/net/html"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
)
var idDb map[int]string
var idDbLock sync.RWMutex
var dbFileName string = "zhihu.db"
var maxTodo int = 0x4000
var thresholdTodo int = maxTodo - 1384
var lenTodo int = 0
var maxProcessor int = 3
var useRand bool = true
var maxValidId int = -1
var tmpId int = -1
var maxGenRandId int = 40
var bitMap *bitmap.Bitmap
type record struct {
id int
title string
}
func main() {
var id int
var file *os.File
result := make(chan record)
todo := make(chan int, maxTodo)
idDb = make(map[int]string)
sem := make(chan int, maxProcessor)
//args := os.Args[1:]
//if args != nil {
// val, err := strconv.Atoi(args[0])
// if err == nil && val > 0 {
// useRand = false
// maxValidId = val
// tmpId = maxValidId
// }
//}
bitMap = bitmap.NewBitmapSize(50000000)
file, err := os.OpenFile(dbFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("[%s]: %s\n", dbFileName, err.Error())
return
}
defer file.Close()
totalLoaded, _ := loadRecord(file)
fmt.Printf("Load %d record(s) from %s\n", totalLoaded, dbFileName)
start := genStartId()
fmt.Println("Start id:", start)
todo <- start
go recorder(file, result)
timeout := make(chan bool)
go func() {
time.Sleep(10 * time.Second)
timeout <- true
}()
for {
lenTodo = len(todo)
select {
case id = <-todo:
case <-timeout:
if lenTodo < 2000 {
fmt.Println("genRandId triggerd")
go genRandId(todo)
}
go func() {
time.Sleep(10 * time.Second)
timeout <- true
}()
continue
}
//fmt.Println("get", id)
idDbLock.RLock()
_, ok := idDb[id]
idDbLock.RUnlock()
if ok {
continue
}
sem <- 1
go func() {
nid := id
url := fmt.Sprintf("http://www.zhihu.com/question/%d", nid)
processUrl(url, result, todo)
<-sem
}()
}
}
func genStartId() int {
var id int
rand.Seed(time.Now().UnixNano())
for {
if useRand {
id = rand.Intn(15734175) + 19550225
} else {
id = tmpId
tmpId--
}
if id <= 0 {
panic("id <= 0")
}
if bitMap.GetBit(uint64(id)) != 0 {
continue
}
bitMap.SetBit(uint64(id), 1)
idDbLock.RLock()
_, ok := idDb[id]
idDbLock.RUnlock()
if ok {
continue
}
url := fmt.Sprintf("http://www.zhihu.com/question/%d", id)
resp, err := http.Get(url)
if err != nil {
continue
}
if resp.StatusCode == 200 {
resp.Body.Close()
break
}
// fmt.Printf("[%d]: %s\n", resp.StatusCode, url)
resp.Body.Close()
}
return id
}
func genRandId(todo chan int) {
var id int
count := maxGenRandId
rand.Seed(time.Now().UnixNano())
for count > 0 {
id = rand.Intn(16000000) + 19550000
if bitMap.GetBit(uint64(id)) != 0 {
continue
}
bitMap.SetBit(uint64(id), 1)
idDbLock.RLock()
_, ok := idDb[id]
idDbLock.RUnlock()
if ok {
continue
}
url := fmt.Sprintf("http://www.zhihu.com/question/%d", id)
resp, err := http.Get(url)
if err != nil {
continue
}
statusCode := resp.StatusCode
resp.Body.Close()
if statusCode == 200 {
count--
todo <- id
}
time.Sleep(10 * time.Millisecond)
}
fmt.Println("genRandId stopped")
}
func recorder(file *os.File, result chan record) {
for {
r := <-result
idDbLock.Lock()
val, ok := idDb[r.id]
if !ok {
// 记录新的id和title
fmt.Printf("[%4d] %d %s\n", lenTodo, r.id, r.title)
wbuf := fmt.Sprintln(r.id, r.title)
_, err := file.WriteString(wbuf)
if err != nil {
fmt.Printf("[%d %s]: %s\n", r.id, r.title, err.Error())
} else {
idDb[r.id] = r.title
}
} else {
// 已经有记录了
if val != r.title {
fmt.Printf("[%d %s]: WARNING new title %s\n", r.id, val, r.title)
}
}
idDbLock.Unlock()
}
}
func loadRecord(file *os.File) (totalLoaded int, lastId int) {
s := bufio.NewScanner(file)
var r string
totalLoaded = 0
lastId = -1
idDbLock.Lock()
for s.Scan() {
r = s.Text()
idx := strings.Index(r, " ")
if idx == -1 {
fmt.Println("Invalid record:", r)
continue
}
id := r[:idx]
title := r[idx+1:]
dec, err := strconv.Atoi(id)
if err != nil {
fmt.Println("Invalid record:", r)
continue
}
val, ok := idDb[dec]
if ok {
fmt.Println("Already exist:", dec, val)
} else {
idDb[dec] = title
}
bitMap.SetBit(uint64(dec), 1)
lastId = dec
totalLoaded++
}
idDbLock.Unlock()
if err := s.Err(); err != nil {
fmt.Println(err.Error())
}
return totalLoaded, lastId
}
func processUrl(url string, result chan record, todo chan int) int {
resp, err := http.Get(url)
if err != nil {
fmt.Printf("[%s]: %s\n", url, err.Error())
return -1
}
defer resp.Body.Close()
z := html.NewTokenizer(resp.Body)
title := "N/A"
for {
tt := z.Next()
if tt == html.ErrorToken {
break
}
t := z.Token()
if t.Type == html.StartTagToken && t.Data == "title" {
tt = z.Next()
t = z.Token()
if t.Type == html.TextToken {
title = t.Data
break
}
}
}
if title == "N/A" {
fmt.Printf("[%s]: Not find title\n", url)
return -1
}
title = strings.TrimSpace(title)
title = strings.Replace(title, "\n", " ", -1)
id, success := parseZhRef(url)
if success {
var r record
r.id = id
r.title = title
result <- r
} else {
fmt.Printf("[%s]: Not find id\n", url)
}
if lenTodo > thresholdTodo {
return 0
}
doc, err := html.Parse(resp.Body)
if err != nil {
fmt.Printf("[%s]: %s\n", url, err.Error())
return -1
}
var f func(*html.Node)
f = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "a" {
attr := n.Attr
for _, a := range attr {
if a.Key == "href" {
id, success := parseZhRef(a.Val)
if !success {
break
}
//if bitMap.GetBit(uint64(id)) != 0 {
// continue
//}
//bitMap.SetBit(uint64(id), 1)
todo <- id
//fmt.Println("added", id)
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(doc)
return 0
}
func parseZhRef(ref string) (int, bool) {
var id string
var idx int
if strings.HasPrefix(ref, "http://www.zhihu.com/question/") {
id = strings.TrimPrefix(ref, "http://www.zhihu.com/question/")
} else if strings.HasPrefix(ref, "/question/") {
id = strings.TrimPrefix(ref, "/question/")
} else {
return -1, false
}
if idx = strings.Index(id, "?"); idx != -1 {
id = id[:idx]
}
if idx = strings.Index(id, "#"); idx != -1 {
id = id[:idx]
}
if idx = strings.Index(id, "/answer"); idx != -1 {
id = id[:idx]
}
if idx = strings.Index(id, "/"); idx != -1 {
id = id[:idx]
}
dec, err := strconv.Atoi(id)
if err != nil {
fmt.Printf("Invalid id %s in %s: %s\n", id, ref, err.Error())
return -1, false
}
return dec, true
}