存储资源

配合着 fs 可以将爬取到的资源进行存储

var Crawler = require("crawler");
var fs = require("fs");
var c = new Crawler({
  encoding:null,
  jQuery:false,// set false to suppress warning message.
  callback:function(err, res, done){
    if(err){
      console.error(err.stack);
    }else{
      fs.createWriteStream(res.options.filename).write(res.body);
    }
    done();
  }
});
c.queue({
  uri:"http://www.baidu.com",
  filename:"baidu.html"
});

同样地,如果是图片资源的话:

var Crawler = require("crawler");
var fs = require("fs");
var c = new Crawler({
  encoding:null,
  jQuery:false,// set false to suppress warning message.
  callback:function(err, res, done){
    if(err){
      console.error(err.stack);
    }else{
      fs.createWriteStream(res.options.filename).write(res.body);
    }
    done();
  }
});
c.queue({
  uri:"https://nodejs.org/static/images/logo.svg",
  filename:"nodejs.svg"
});

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress