Nodejs实现批量下载妹纸图
作者:bea
听说最近下载妹子图很火? Nodejs (javascrpt)自然不能落后~ 虽然从没写过像样的Nodejs程序,但作为至少翻过书的前端同学来说,Nodejs用得还蛮顺手的哈~ 花了一点事件学习了下Nodejs的网页获取和文件下载方法,没事乱捣腾就写了这个半成品的下载器 使用方法: 1)新建一个download目录 2)新建download.js(其实名字随便取),并复制到download目录下 3)复制两段代码到download.js中 4)打开命令行工具,并将当前目
听说最近下载妹子图很火?
Nodejs (javascrpt)自然不能落后~
虽然从没写过像样的Nodejs程序,但作为至少翻过书的前端同学来说,Nodejs用得还蛮顺手的哈~ 花了一点事件学习了下Nodejs的网页获取和文件下载方法,没事乱捣腾就写了这个半成品的下载器
使用方法:
1)新建一个download目录 2)新建download.js(其实名字随便取),并复制到download目录下 3)复制两段代码到download.js中 4)打开命令行工具,并将当前目录转到与download目录下 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: /<imgs*src="(.*?)"s*alt=".*"s*/>/g,
total: 141,
from: 1
});
mzitu.start();
以上所述就是本文的全部内容了,希望大家能够喜欢。
有用 | 无用
Nodejs (javascrpt)自然不能落后~
虽然从没写过像样的Nodejs程序,但作为至少翻过书的前端同学来说,Nodejs用得还蛮顺手的哈~ 花了一点事件学习了下Nodejs的网页获取和文件下载方法,没事乱捣腾就写了这个半成品的下载器
使用方法:
1)新建一个download目录 2)新建download.js(其实名字随便取),并复制到download目录下 3)复制两段代码到download.js中 4)打开命令行工具,并将当前目录转到与download目录下 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: /<imgs*src="(.*?)"s*alt=".*"s*/>/g,
total: 141,
from: 1
});
mzitu.start();
以上所述就是本文的全部内容了,希望大家能够喜欢。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JQuery+CSS实现图片上放置按钮的方法
- Jquery解析json字符串及json数组的方法
- js实现时间显示几天前、几小时前或者几分钟前的方法集锦
- jQuery实现返回顶部效果的方法
- jquery读取xml文件实现省市县三级联动的方法
- Jquery动态添加输入框的方法
- jquery任意位置浮动固定层插件用法实例
- JQuery控制Radio选中方法分析
- 究竟什么是Node.js?Node.js有什么好处?
- js实现发送验证码后的倒计时功能
- JS实现两表格里数据来回转移的方法
- 原生JS和JQuery动态添加、删除表格行的方法
- jQuery实现html表格动态添加新行的方法
- jquery实现用户打分评分特效
- nodejs实现遍历文件夹并统计文件大小
- 用Node.js通过sitemap.xml批量抓取美女图片
- javascript转换静态图片,增加粒子动画效果
- jQuery实现限制textarea文本框输入字符数量的方法
- nodejs实现获取某宝商品分类