国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

用Node.js通過sitemap.xml批量抓取美女圖片

2019-11-20 12:23:56
字體:
來源:轉載
供稿:網友

之前看了很多個版本,自己也搞一個。

1. 支持指定保存到哪個目錄
2. 按文章進行分目錄存放
3. 支持設置并行下載上限

下次有空再搞個整站下載的。

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 = {   /**   * 配置選項   */  options: {    // 網站sitemap地址    sitemap: 'http://sexy.faceks.com/sitemap.xml',    // 保存到此文件夾    saveTo: '/Users/Fay/Pictures/me2sex',    // 圖片并行下載上限    downLimit: 5,  },   posts: [],   /**   * 開始下載(程序入口函數)   */  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: 下載成功');      }    });  },   /**   * 包裹任務,確保原任務的上下文指向某個特定對象   * @param {Function} task 符合asycs.js調用方式的任務函數   * @param {Any} context 上下文   * @param {Array} exArgs 額外的參數   * @return {Function} 符合asycs.js調用方式的任務函數   */  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);    };  },   /**   * 獲取站點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轉成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個頁面', 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);  },    /**   * 下載單個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('準備創建目錄:%s', post.dir);    if (node.fs.existsSync(post.dir)) {      callback(null, post);      console.log('目錄:%s 已經存在', post.dir);      return;    }    node.mkdirp(post.dir, function(err) {      callback(err, post);      console.log('目錄:%s 創建成功', post.dir);    });  },   /**   * 獲取post內容   */  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('發現%d張妹子圖片,準備開始下載...', post.images.length);    node.async.eachLimit(      post.images,      this.options.downLimit,      this.wrapTask(this.downImage, this, [post]),      callback    );  },   /**   * 下載單個圖片   */  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();

以上所述就是本文的全部內容,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凉山| 惠来县| 华安县| 兴安县| 东阳市| 朝阳区| 青河县| 潍坊市| 托克逊县| 民丰县| 陆良县| 攀枝花市| 菏泽市| 昌邑市| 岫岩| 志丹县| 甘孜县| 永安市| 福海县| 江山市| 苍溪县| 介休市| 平武县| 富蕴县| 辽阳市| 安化县| 大埔区| 裕民县| 江北区| 交口县| 遂昌县| 东港市| 彰化县| 柳州市| 南宫市| 安福县| 从化市| 来宾市| 廉江市| 兰西县| 三门县|