ホームページ >ウェブフロントエンド >jsチュートリアル >Nodejs は、girl photos_node.js の一括ダウンロードを実装します。
最近、女の子の写真をダウンロードするのが流行っていると聞きましたが?
Nodejs (javascrpt) は当然遅れを取ることはできません~
私はまともな Nodejs プログラムを書いたことはありませんが、少なくとも本を読んだフロントエンドの学生として、Nodejs を非常に快適に使用できます~
Nodejs で Web ページを取得してファイルをダウンロードする方法を少し時間をかけて学習し、何もいじる必要がなかったときにこの半完成のダウンローダーを作成しました
使用法:
1) 新しいダウンロード ディレクトリを作成します
2) 新しい download.js を作成し (実際には任意の名前を付けます)、ダウンロード ディレクトリ
にコピーします。
3) 2 つのコードを download.js
にコピーします。
4) コマンドラインツールを開き、現在のディレクトリをダウンロードディレクトリ
に変更します。
5) コマンドラインに「node download.js
」と入力します。
6) 女の子の写真を待っています~
シンプルな女の子の写真オブジェクト (自動ダウンロードの新規サポート)
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(); } };
女の子の写真のダウンロードを開始する方法
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();
以上がこの記事の全内容です。皆さんに気に入っていただければ幸いです。