-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler_trending.js
353 lines (315 loc) · 11.2 KB
/
crawler_trending.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
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
// 使用 superagent 模块 代理请求网页 + cheerio爬取节点数据
const request = require('superagent');
var htmlparser = require("htmlparser2");
const cheerio = require('cheerio');
var mongoose = require('./model/db');
// var mongoose = require('mongoose');
var Article_Model = mongoose.model('Article'); // 使用User模型
var ArticleDetail = mongoose.model('ArticleDetail');
// TODO:爬取简书7日热门
function getPage(page) {
var pageNO = page;
if (page) {
pageNO = 1;
var queryPage = "?page=" + page
console.log(`onQueryPage.`);
} else {
queryPage = ""
console.log('default pageOne.');
}
request
.get('http://www.jianshu.com/trending/weekly'+ queryPage)
// .query({ action: 'edit', city: 'London' }) // query string
// .use(prefix) // Prefixes *only* this request
// .use(nocache) // Prevents caching of *only* this request
.end(function (err, res) {
// Do something
if (err) {
console.log('request failed.' + err);
} else {
// 返回的text
var show = contentFilter(res.text, pageNO);
// 遍历本地数据爬取详情页
crawler2detail(show);
console.log(`首页(head/request/response/html):` + res);
console.log('\n\n\n========================all response divider =============================\n\n\n');
console.log(show);
}
});
}
function contentFilter(page, pageNO) {
var $ = cheerio.load(page);
// 文章列表
var list = $('.note-list');
var data = [];
// 注意: 必须find出note-list下的每一个 li 节点, 再each取值
// 遍历每个item, 取出 作者, 标题, 概要, 日期, 头像
list.find('li').each(function (item) {
var article = $(this);
var content = article.find('.content');
// console.log(content + '\n')
// var author = content.find('.author').text();
// #note-13049926 > div > div.author > a
var avatar = content.find('.author a img').prop('src');
// $('.apple', '#fruits').text()
var author = content.find('.author .name a').text().trim();
var time = content.find('.author .name .time').prop('data-shared-at');
// $('#fruits').children().last().text();
// #note-13072607 > div > div.author > div > span
// console.log(author + '\n')
var title = content.find('.title').text().trim();
// console.log(title + '\n')
var abstract = content.find('.abstract').text().trim();
// console.log(abstract + '\n')
var href = content.find('.meta a').prop('href');
console.log(href);
// 对象赋值
var articleData = {
Author: author,
Title: title,
Abstract: abstract,
Date: time,
Avatar: avatar,
href: href
};
data.push(articleData);
// 更具Model创建数据实体
new Article_Model({
author: author,
title: title,
_abstract: abstract,
date: time,
avatar: avatar,
href: href,
page: pageNO
}).save(function (err, user, count) {
// ctx.redirect('/');
console.log('简书7日热门 入库成功...')
});
});
return data;
}
function articleFilter(articleData, href) {
var $ = cheerio.load(articleData);
// 文章列表
// body > div.note > div.post > div.article > h1
var list = $('div.note');
// console.log(list);
var data = [];
var title = list.find('div.post div.article h1').text().trim();
console.log('标题: ' + title);
// body > div.note > div.post > div.article > div.author > a > img
var avatar = list.find('div.post div.article div.author a img').prop('src');
console.log('头像: ' + avatar);
// body > div.note > div.post > div.article > div.author > div > span.name > a
var name = list.find('div.post div.article div.author div span.name a').text().trim();
console.log('昵称: ' + name);
// body > div.note > div.post > div.article > div.show-content
// var content = list.find('div.post div.article div.show-content').find('p');
var content = list.find('div.post div.article div.show-content');
var parseContent = '';
var parser = new htmlparser.Parser({
onopentag: function (name, attribs) {
// if(name === "script" && attribs.type === "text/javascript"){
// console.log("JS! Hooray!");
// }
if (name === "p") {
console.log("p tag!");
parseContent = parseContent + '<p>';
} else if (name == 'i') {
parseContent = parseContent + '<i>';
} else if (name == 'div') {
parseContent = parseContent + '<div>';
} else if (name == 'img') {
parseContent = parseContent + '<img>';
} else if (name == 'h1') {
parseContent = parseContent + '<h1>';
} else if (name == 'b') {
parseContent = parseContent + '<b>';
} else if (name == 'br') {
parseContent = parseContent + '<br>';
}
},
ontext: function (text) {
console.log("-->", text);
parseContent = parseContent + text;
},
onclosetag: function (tagname) {
if (tagname === "p") {
console.log("p tag end!");
parseContent = parseContent + '</p>';
} else if (name == 'i') {
parseContent = parseContent + '</i>';
} else if (name == 'h1') {
parseContent = parseContent + '</h1>';
} else if (name == 'b') {
parseContent = parseContent + '</b>';
} else if (name == 'br') {
parseContent = parseContent + '<br>';
}
}
}, {decodeEntities: true});
// parser.write(content);
// parser.end();
// console.log(`===========================\n` + parseContent + `=============================\n`);
// console.log('================================================');
// console.log('正文: ' + content);
// console.log("================================================");
// 发表时间, 字数
var pub_time = list.find('div.post div.article div.author div div span.publish-time').text();
console.log(pub_time);
var wordage = list.find('div.post div.article div.author div div span.wordage').text();
console.log(wordage);
new ArticleDetail({
author: name,
title: title,
text: content,
date: pub_time,
avatar: avatar,
wordage: wordage,
href: href
}).save(function (err, res, count) {
if (err) {
console.log(err);
return;
} else {
console.log(res);
console.log(count);
}
});
}
function crawler2detail(collection) {
for (v of collection) {
console.log('========== page_href: ' + v.href + ' ==============');
const href = v.href;
request.get('http://www.jianshu.com' + v.href)
.end(function (err, resp) {
if (err) {
console.log(err);
return;
} else {
// console.log(resp.text);
articleFilter(resp.text, href);
}
});
}
}
function queryDatabase() {
ArticleDetail.find(function (err, list) {
if (err)
console.log('failed...')
else {
console.log('result:' + list);
}
});
}
function countArtical() {
Article_Model.count(function (err, num) {
if (err)
console.log('failed...')
else {
console.log('count:' + num);
}
});
}
function countArticalDetail() {
ArticleDetail.count(function (err, num) {
if (err)
console.log('failed...')
else {
console.log('count:' + num);
}
});
}
function queryArticleDetailByAuthor(author) {
ArticleDetail.findOne({'author': author}, function (err, item) {
if (err)
console.log('failed...');
else
console.log('result: ' + item);
})
}
function queryArticleDetailByhref(href) {
ArticleDetail.findOne({'href': href}, function (err, item) {
if (err)
console.log('failed...');
else
console.log('result: ' + item);
})
}
// 根据 href字段 查询单条信息
function queryByhref(href) {
Article_Model.findOne({"href": href}, (err, item) => {
if (err)
console.log('failed...')
else
console.log('result: ' + item)
})
}
function clearDatabase() {
// 移除整个collection数据表
ArticleDetail.remove({}, function (err) {
if (err)
console.log(err);
else
console.log('drop collection ArticleDetail success...');
});
Article_Model.remove({}, function (err) {
if (err)
console.log(err);
else
console.log('drop collection Article_Model success...');
});
}
function removeByAuthor(str) {
if (!str)
return;
else
var author = str;
var condition = {
$or: [{"author": author}] // 条件满足其一
// $and: [{"username": usr}, {"goodAt": good}] // 同时满足条件
}
Article_Model.findOneAndRemove({"author": author}, function (err, doc) {
if (err) {
console.log(err)
return;
}
console.log('remove success...');
})
}
function getMultiPage(count) {
for (var page = 0; page < count; page++) {
// async function crawler() {
// await
getPage(page+1)
// }
}
}
// clearDatabase()
getMultiPage(2)
// getPage()
// 实现分页的关键DOM节点
// <a data-page="3" href="/trending/weekly" class="load-more">阅读更多</a>
// <meta name="mobile-agent" content="format=html5;url=https://www.jianshu.com/trending/weekly?page=2">
// Chrome调试 可以发现request header带 page=num 参数
// ?page=2 query参数是关键
/**
* phantomJS模拟翻页:鼠标滑动到最后一个元素
*/
// clearDatabase();
// 查看 db.articles.find()
// 删除collections CLI: db.articles.drop()
// removeByAuthor('阿阳sunny');
// queryDatabase();
// countArtical();
// countArticalDetail();
// queryByhref('/p/d315a19a991a');
// queryArticleDetailByAuthor('瓯南');
// queryArticleDetailByhref('/p/c5bc31531ecb');
// updateCollections()
function updateCollections() {
// Article collection and update "abstract" column
db.getCollection('Article').update({}, {$rename : {"abstract" : "_abstract"}}, false, true)
// Schema.findOneAndUpdate({_id: id},{test: “更新字段”}).exex();
}