筆趣閣小說爬蟲

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: 寫這個的緣由是想在個人代碼編輯器裏看小說,上班的時候比較安全
相關文章
相關標籤/搜索