2019-04-04

Nodejs爬虫初试探


效果.gif

代码

var http = require('http');
var fs = require('fs');
var url = 'http://moe.005.tv/77510.html';
http.get(url,function(res){
var data = '';
res.on('data',function(a){
data += a.toString();
})
res.on('end',function(){
var reg = /.+?/img;
var arr = [];
var result = '';
while(result = reg.exec(data)){
arr.push(result[1]);
}
for(var i in arr){
var len = arr[i].length;
var last = arr[i][len-3];
if(last == 'j') var suffix = '.jpg';
else var suffix = '.png';
http.get(arr[i],function(res){
var name = new Date().getTime();
var stream = fs.createWriteStream('./动漫壁纸/' + name + suffix);
res.pipe(stream);
})
}
})
})

你可能感兴趣的:(2019-04-04)