Home >Web Front-end >JS Tutorial >Nodejs implements batch downloading of girl pictures_node.js
I heard that downloading pictures of girls is very popular recently?
Nodejs (javascrpt) naturally cannot lag behind~
Although I have never written a decent Nodejs program, as a front-end student who has at least read the book, I am quite comfortable using Nodejs~
I spent a little time learning how to obtain web pages and download files in Nodejs, and I wrote this semi-finished downloader when I had nothing to mess around with
Usage:
1) Create a new download directory
2) Create a new download.js (actually name it whatever you want) and copy it to the download directory
3) Copy the two pieces of code into download.js
4) Open the command line tool and change the current directory to the download directory
5) Enter in the command line: node download.js
6) Waiting to receive pictures of girls~
Simple girl picture object (newly added support for automatic 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(); } };
How to start downloading girl pictures
var mzitu = new Mzitu({ baseUrl: 'http://www.mzitu.com/share/comment-page-', dir: '', reg: /<img\s*src="(.*?)"\s*alt=".*"\s*\/>/g, total: 141, from: 1 }); mzitu.start();
The above is the entire content of this article, I hope you all like it.