Nodejs implements batch download of sister-in-law drawings and nodejs sister-in-law drawings
I heard that the recently downloaded girl figure is very popular?
Nodejs (javascrpt) cannot lag behind ~
Although I have never written a decent Nodejs program, as a front-end student who has at least read the book, Nodejs is quite handy ~
I spent a little time learning about node. js's webpage acquisition and File Download methods. I wrote this semi-finished download tool when I had nothing to worry about.
Usage:
1) create a download directory
2) create a new download. js (in fact, the name can be retrieved at Will) and copy it to the download directory.
3) Copy two pieces of code to download. js.
4) Open the command line tool and convert the current directory to the directory with download
5) Enter node download. js in the command line.
6) Wait for the photo to be taken ~
Simple sister chart object (new support for determining download)
var http = require('http');var fs = require('fs'); function Mzitu(options) { this.id = 1; this.initialize.call(this, options); return this;} Mzitu.prototype = { constructor: Mzitu, initialize: function _initialize(options) { this.baseUrl = options.baseUrl; this.dir = options.dir || ''; this.reg = options.reg; this.total = options.total; this.page = options.from || 1; }, start: function _start() { this.getPage(); }, getPage: function _getPage() { var self = this, data = null; if (this.page <= this.total) { http.get(this.baseUrl + this.page, function (res) { res.setEncoding("utf8"); res.on('data', function (chunk) { data += chunk; }).on('end', function () { self.parseData(data); }); }); } }, parseData: function _parseData(data) { var res = [], match; while ((match = this.reg.exec(data)) != null) { res.push(match[1]); } this.download(res); }, download: function _download(resource) { var self = this, currentPage = self.page; resource.forEach(function (src, idx) { var filename = src.substring(src.lastIndexOf('/') + 1), writestream = fs.createWriteStream(self.dir + filename); http.get(src, function (res) { res.pipe(writestream); }); writestream.on('finish', function () { console.log('page: ' + currentPage + ' id: ' + self.id++ + ' download: ' + filename); }); }); self.page++; self.getPage(); }};
Sister figure download startup Method
var mzitu = new Mzitu({ baseUrl: 'http://www.mzitu.com/share/comment-page-', dir: '', reg: //g, total: 141, from: 1}); mzitu.start();
The above is all the content of this article. I hope you will like it.