-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (38 loc) · 1.13 KB
/
index.js
File metadata and controls
38 lines (38 loc) · 1.13 KB
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
var axios = require('axios');
var cheerio = require('cheerio');
var fs = require('fs');
var siteUrl = 'https://segmentfault.com';
var baseUrl = 'https://segmentfault.com/questions/hottest';
var questions = [];
questionFun(baseUrl);
function questionFun(url) {
axios.get(url)
.then(function (response) {
var $ = cheerio.load(response.data);
$('.question-stream .stream-list__item').each(function () {
var elem = $(this);
questions.push({
"title": elem.find('.summary .title a').text().trim(),
"url": siteUrl + elem.find('.summary .title a').attr('href').trim()
});
});
var next = $('.text-center .pagination .next a').attr('href');
if(next) {
questionFun(siteUrl + next);
} else {
// console.log(questions);
var newQuestions = JSON.stringify(questions);
fs.writeFile('data.js', newQuestions, function (err) {
if(err) {
console.log('文件写入失败');
} else {
console.log('文件写入成功');
}
});
fs.appendFileSync('data1.js', newQuestions);
}
})
.catch(function (error) {
console.log(error);
});
}