-
Notifications
You must be signed in to change notification settings - Fork 17
/
demo2.js
77 lines (67 loc) · 2.09 KB
/
demo2.js
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
let fs = require('fs');
let _ = require('lodash');
let util = require('./util/io');
let interactive = require('./util/interactive');
function crawlerMaintopics (toFile) {
toFile = toFile || './mainTypes.json';
let nightmare = new Nightmare();
interactive
.iMainTopic(url)
.then(value => {
util.writeJSONToFile(value, toFile, _ => {
console.log('finish crawler mainTopics');
})
})
}
async function crawlerTopics (toFile) {
let mainTopics = require('./mainTopics.json').mainTopics;
toFile = toFile || './topics.json';
let topics = [];
let delay = 800;
for (let i = 0; i < mainTopics.length; i += 1) {
topics.push(await interactive.iSubTopic(mainTopics[i], delay));
}
topics = _.flatten(topics);
topics = {
topics: topics,
};
util.writeJSONToFile(topics, toFile, _ => {
console.log('finish crawler topics');
});
}
async function crawlerTopicsDetail (sourceFile, toFile) {
sourceFile = sourceFile || './topics.json';
toFile = toFile || './topicsDetail.json';
let sourceData = require(sourceFile);
let topics = sourceData.topics;
for (let i = 0; i < topics.length; i += 1) {
try {
await interactive.iTopicDesc(topics[i]);
} catch (error) {
console.log(topics['id'], 'exists problem');
}
}
interactive.close();
util.writeJSONToFile(sourceData, toFile, _ => {
console.log('finished crawler topics detail');
});
}
/**
*
* @param {string} rootUrl - 顶层的url
* @param {int} deep - 抓取页面的深度
* @param {string} toFile - 保存的深度
* @param {Function} cb
*/
async function crawlerTopicsFromRoot (rootUrl, deep, toFile, cb) {
rootUrl = rootUrl ||'https://www.zhihu.com/topic/19776749/hot'
toFile = toFile || './topicsTree.json'
console.time();
let result = await interactive
.iAllTopics(rootUrl, deep);
console.timeEnd();
util.writeJSONToFile(result['topics'], toFile, cb);
}
crawlerTopicsFromRoot('', 2, '', _ => {
console.log('完成抓取');
})