用Node.js通過sitemap.xml批量抓取美女圖片
之前看了很多個(gè)版本,自己也搞一個(gè)。
1. 支持指定保存到哪個(gè)目錄
2. 按文章進(jìn)行分目錄存放
3. 支持設(shè)置并行下載上限
下次有空再搞個(gè)整站下載的。
package.json
{
"name": "me2sex-images",
"version": "0.0.1",
"description": "Batch download images from http://me2-sex.lofter.com",
"main": "index.js",
"author": "Fay",
"license": "MIT",
"dependencies": {
"async": "^0.9.0",
"cheerio": "^0.18.0",
"mkdirp": "^0.5.0",
"request": "^2.51.0",
"url": "^0.10.2",
"xml2js": "^0.4.4"
}
}
index.js
var node = {
async: require('async'),
cheerio: require('cheerio'),
fs: require('fs'),
mkdirp: require('mkdirp'),
path: require('path'),
request: require('request'),
url: require('url'),
xml2js: require('xml2js'),
};
var Me2SexImages = {
/**
* 配置選項(xiàng)
*/
options: {
// 網(wǎng)站sitemap地址
sitemap: 'http://sexy.faceks.com/sitemap.xml',
// 保存到此文件夾
saveTo: '/Users/Fay/Pictures/me2sex',
// 圖片并行下載上限
downLimit: 5,
},
posts: [],
/**
* 開始下載(程序入口函數(shù))
*/
start: function() {
var self = this;
var async = node.async;
async.waterfall([
self.wrapTask(self.sitemapXML),
self.wrapTask(self.sitemapJSON),
self.wrapTask(self.downAllImages),
], function(err, result) {
if (err) {
console.log('error: %s', err.message);
} else {
console.log('success: 下載成功');
}
});
},
/**
* 包裹任務(wù),確保原任務(wù)的上下文指向某個(gè)特定對象
* @param {Function} task 符合asycs.js調(diào)用方式的任務(wù)函數(shù)
* @param {Any} context 上下文
* @param {Array} exArgs 額外的參數(shù)
* @return {Function} 符合asycs.js調(diào)用方式的任務(wù)函數(shù)
*/
wrapTask: function(task, context, exArgs) {
var self = this;
return function() {
var args = [].slice.call(arguments);
args = exArgs ? exArgs.concat(args) : args;
task.apply(context || self, args);
};
},
/**
* 獲取站點(diǎn)sitemap.xml
*/
sitemapXML: function(callback) {
console.log('開始下載sitemap.xml');
node.request(this.options.sitemap, function(err, res, body) {
if (!err) console.log('下載sitemap.xml成功');
callback(err, body);
});
},
/**
* 將sitemap.xml轉(zhuǎn)成json
*/
sitemapJSON: function(sitemapXML, callback) {
var self = this;
console.log('開始解析sitemap.xml');
node.xml2js.parseString(sitemapXML, {explicitArray: false}, function(err, json) {
if (!err) {
self.posts = json.urlset.url;
self.posts.shift();
console.log('解析sitemap.xml成功,共有%d個(gè)頁面', self.posts.length);
}
callback(err, self.posts);
});
},
/**
* 下載整站圖片
*/
downAllImages: function(callback) {
var self = this;
var async = node.async;
console.log('開始批量下載');
async.eachSeries(self.posts, self.wrapTask(self.downPostImages), callback);
},
/**
* 下載單個(gè)post的圖片
* @param {Object} post 文章
*/
downPostImages: function(post, callback) {
var self = this;
var async = node.async;
async.waterfall([
self.wrapTask(self.mkdir, self, [post]),
self.wrapTask(self.getPost),
self.wrapTask(self.parsePost),
self.wrapTask(self.downImages),
], callback);
},
mkdir: function(post, callback) {
var path = node.path;
var url = node.url.parse(post.loc);
post.dir = path.join(this.options.saveTo, path.basename(url.pathname));
console.log('準(zhǔn)備創(chuàng)建目錄:%s', post.dir);
if (node.fs.existsSync(post.dir)) {
callback(null, post);
console.log('目錄:%s 已經(jīng)存在', post.dir);
return;
}
node.mkdirp(post.dir, function(err) {
callback(err, post);
console.log('目錄:%s 創(chuàng)建成功', post.dir);
});
},
/**
* 獲取post內(nèi)容
*/
getPost: function(post, callback) {
console.log('開始請求頁面:%s', post.loc);
node.request(post.loc, function(err, res, body) {
if (!err) post.html = body;
callback(err, post);
console.log('請求頁面成功:%s', post.loc);
});
},
/**
* 解析post,并獲取post中的圖片列表
*/
parsePost: function(post, callback) {
var $ = post.$ = node.cheerio.load(post.html);
post.images = $('.img')
.map(function() {return $(this).attr('bigimgsrc');})
.toArray();
callback(null, post);
},
/**
* 下載post圖片列表中的圖片
*/
downImages: function(post, callback) {
console.log('發(fā)現(xiàn)%d張妹子圖片,準(zhǔn)備開始下載...', post.images.length);
node.async.eachLimit(
post.images,
this.options.downLimit,
this.wrapTask(this.downImage, this, [post]),
callback
);
},
/**
* 下載單個(gè)圖片
*/
downImage: function(post, imgsrc, callback) {
var url = node.url.parse(imgsrc);
var fileName = node.path.basename(url.pathname);
var toPath = node.path.join(post.dir, fileName);
console.log('開始下載圖片:%s,保存到:%s,文件名:%s', imgsrc, post.dir, fileName);
node.request(imgsrc)
.pipe(node.fs.createWriteStream(toPath))
.on('close', function() {
console.log('圖片下載成功:%s', imgsrc);
callback();
})
.on('error', callback);
}
};
Me2SexImages.start();
以上所述就是本文的全部內(nèi)容,希望大家能夠喜歡。
相關(guān)文章
js設(shè)計(jì)模式之代理模式及訂閱發(fā)布模式實(shí)例詳解
這篇文章主要介紹了js設(shè)計(jì)模式之代理模式及訂閱發(fā)布模式,結(jié)合實(shí)例形式詳細(xì)分析了代理模式及訂閱發(fā)布模式的概念、原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-08-08
JavaScript 文件加載與阻塞問題之性能優(yōu)化案例詳解
這篇文章主要介紹了JavaScript 文件加載與阻塞問題之性能優(yōu)化案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Javascript實(shí)現(xiàn)簡單的富文本編輯器附演示
這篇文章主要介紹了通過Javascript實(shí)現(xiàn)的簡單富文本編輯器,需要的朋友可以參考下2014-06-06
Javascript封裝DOMContentLoaded事件實(shí)例
這篇文章主要介紹了Javascript封裝DOMContentLoaded事件實(shí)例,DOMContentLoaded是FF,Opera 9的特有的Event, 當(dāng)所有DOM解析完以后會(huì)觸發(fā)這個(gè)事件,需要的朋友可以參考下2014-06-06
javascript使用遞歸算法求兩個(gè)數(shù)字組合功能示例
這篇文章主要介紹了javascript使用遞歸算法求兩個(gè)數(shù)字組合功能,結(jié)合實(shí)例形式分析了JS基于遞歸算法的數(shù)組遍歷、判斷、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
每個(gè) JavaScript 工程師都應(yīng)懂的33個(gè)概念
這個(gè)項(xiàng)目是為了幫助開發(fā)者掌握 JavaScript 概念而創(chuàng)立的。它不是必備,但在未來學(xué)習(xí)( JavaScript )中,可以作為一篇指南,需要的朋友可以參考下2018-10-10
Html5 js實(shí)現(xiàn)手風(fēng)琴效果
這篇文章主要為大家詳細(xì)介紹了Html5 js實(shí)現(xiàn)手風(fēng)琴效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
解決webpack dev-server不能匹配post請求的問題
這篇文章主要介紹了解決webpack不能匹配post請求的問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
JavaScript常見數(shù)組方法之如何轉(zhuǎn)置矩陣
這篇文章主要給大家介紹了關(guān)于JavaScript常見數(shù)組方法之如何轉(zhuǎn)置矩陣的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03

