您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

用Node.js通过sitemap.xml批量抓取美女图片

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   这篇文章主要介绍了用Node.js通过sitemap.xml批量抓取美女图片的方法和相关代码,有需要的小伙伴可以参考下。

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

  1. 支持指定保存到哪个目录

  2. 按文章进行分目录存放

  3. 支持设置并行下载上限

  下次有空再搞个整站下载的。

  package.json

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 { "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

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 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;