笔趣阁小说爬虫

var https=require('https');
var fs = require('fs'); //文件模块
var iconv = require('iconv-lite');
var cheerio = require('cheerio');
// 章节目录页面地址
https.get('https://www.bequge.com/11_11694/',function(res){
var length=0;
var arr=[];
res.on("data",function(chunk){
arr.push(chunk);
length+=chunk.length;
});
res.on("end",function(){
var data=Buffer.concat(arr,length);
var change_data = iconv.decode(data,'gb2312');
var $=cheerio.load(change_data.toString());
var listArray = $('dd');
for(var i = 9; i<listArray.length; i++){
setTimeout(function(){
var ddObj = listArray[this.i];
var url = ddObj.children[0].attribs["href"];
var text = ddObj.children[0].children[0].data;
if(text.indexOf("章")==-1){
return;
}
var filePath = "./三寸人间/"+text+".txt";
https.get('https://www.bequge.com'+url,function(response){
var len = 0;
var array = [];
response.on("data",function(chunk){
array.push(chunk);
len+=chunk.length;
});
response.on("end",function(){
var data=Buffer.concat(array,len);
var change_data = iconv.decode(data,'gb2312');
var $=cheerio.load(change_data.toString());
var data = $('#content');
fs.writeFile(filePath, data.text(), { 'flag': 'a' }, function(err) {
if (err) {
throw err;
}
console.log('写入完成.');
});
});
})
}.bind({
i: i
}),2000)
}

})
});
ps: 写这个的原因是想在我的代码编辑器里看小说,上班的时候比较安全
posted @ 2018-11-12 17:25  衡石李  阅读(828)  评论(0编辑  收藏  举报