Nodejs implements batch download of sister paper fig _ node. js

Source: Internet
Author: User
This article mainly introduces the method and detailed code for batch download of sister-in-law drawings using Nodejs. it is very practical and can be referenced by friends who like sister-in-law. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.