Nodejs實(shí)現(xiàn)批量下載妹紙圖
聽(tīng)說(shuō)最近下載妹子圖很火?
Nodejs (javascrpt)自然不能落后~
雖然從沒(méi)寫過(guò)像樣的Nodejs程序,但作為至少翻過(guò)書(shū)的前端同學(xué)來(lái)說(shuō),Nodejs用得還蠻順手的哈~
花了一點(diǎn)事件學(xué)習(xí)了下Nodejs的網(wǎng)頁(yè)獲取和文件下載方法,沒(méi)事亂搗騰就寫了這個(gè)半成品的下載器
使用方法:
1)新建一個(gè)download目錄
2)新建download.js(其實(shí)名字隨便?。?,并復(fù)制到download目錄下
3)復(fù)制兩段代碼到download.js中
4)打開(kāi)命令行工具,并將當(dāng)前目錄轉(zhuǎn)到與download目錄下
5)在命令行中輸入:node download.js
6)等著收妹子圖吧~
簡(jiǎn)單的妹子圖對(duì)象(新增斷定下載支持)
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(); } };
妹子圖下載啟動(dòng)方式
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();
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
node.js報(bào)錯(cuò):npm?ERR?code?EPERM的解決過(guò)程
在學(xué)習(xí)vue+typescript的時(shí)候突然發(fā)現(xiàn)了個(gè)錯(cuò)誤,所以下面這篇文章主要給大家介紹了關(guān)于node.js報(bào)錯(cuò):npm?ERR?code?EPERM的詳細(xì)解決過(guò)程,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08npm?i報(bào)錯(cuò)以及解決方案實(shí)戰(zhàn)案例
npm在前端開(kāi)發(fā)流程中提供了非常完善的自動(dòng)化工具鏈,但是同樣由于其復(fù)雜性導(dǎo)致有很多奇奇怪怪的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于npm?i報(bào)錯(cuò)以及解決方案的相關(guān)資料,需要的朋友可以參考下2022-07-07使用cluster 將自己的Node服務(wù)器擴(kuò)展為多線程服務(wù)器
nodejs在v0.6.x之后 增加了一個(gè)模塊 cluster 用于實(shí)現(xiàn)多進(jìn)程,利用child_process模塊來(lái)創(chuàng)建和管理進(jìn)程,增加程序在多核CPU機(jī)器上的性能表現(xiàn)。本文將介紹利用cluster模塊創(chuàng)建的多線程的問(wèn)題。2014-11-11利用Mongoose讓JSON數(shù)據(jù)直接插入或更新到MongoDB
這篇文章主要給大家介紹了利用Mongoose讓JSON數(shù)據(jù)直接插入或更新到MongoDB數(shù)據(jù)庫(kù)的相關(guān)資料,文中詳細(xì)介紹了配置Mongoose、創(chuàng)建目錄及文件、插入數(shù)據(jù),POST提交JSON增加一條記錄以及詢數(shù)據(jù),取出剛增加的記錄等內(nèi)容,需要的朋友可以參考下。2017-05-05