-
Notifications
You must be signed in to change notification settings - Fork 0
/
zztj.js
56 lines (48 loc) · 1.88 KB
/
zztj.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
const http = require('http');
const fs = require('fs');
const cheerio = require('cheerio');
var i = 0;
var url = 'http://www.gushiwen.org/GuShiWen_d349b8947b.aspx';
function fetchPage(url) {
startRequest(url);
}
function startRequest(url) {
http.get(url, (res) => {
var html = '';
res.setEncoding('utf-8');
res.on('data', (chunk) => {
html += chunk;
});
res.on('end', () => {
i++;
if (i < 10) {
i = "00" + i;
}
else if (i >= 10 && i < 100) {
i = "0" + i;
}
var $ = cheerio.load(html);
var title = i.toString() + $('div.cont h1').text().trim();
var subhead = $('.cont .contson p').first().text().trim();
console.log(title + subhead);
var filename = './data/' + title + '——' + subhead + '.txt';
$('.cont .contson p').each(function (index, items) {
var p = $(this).text();
var y = p.substring(0, 2).trim();
if (y == '') {
p = p + '\n';
fs.appendFile(filename, p, 'utf-8', (err) => {
if (err)
console.log(err);
});
}
});
var nextLink = $('.cont .contson').first().find('p').last().find('strong a').first().attr('href');
console.log("********" + nextLink);
if (i < 294 && nextLink != '') {
fetchPage(nextLink);
}
});
}).on('error', (err) => { console.log(err) });
}
fetchPage(url);