download images with node.js


最初是做个展示百人一首的网页,要相关数据,然后找到了这份json文件:
https://gist.github.com/wakaba/8363dc27f4c54f76b4a7/
json 里的图片 url 是没有规律可以遵循的,没法用下载工具批量下载,
所以最后想到了用node写个脚本扒json然后下载到本地。

var http = require('http'),
    fs = require('fs');

var files = require('data.json'),
    length = datas.length,
    imgs = [],
    i = 0;

while (i < length) {
  imgs.push(files[i].imageURL);
  i++;
}

var download = function(url, dest) {
  var file = fs.createWriteStream(dest);
  http.get(url, function(response) {
    response.pipe(file);
  });
};

imgs.forEach(function(img, index) {
  download(img, String(index + 1) + '.jpg');
});

referrer: stackoverflow – How to download a file with Node.js?

,