首页 > 语言 > JavaScript > 正文

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

2024-05-06 16:20:58
字体:大 中 小
来源:转载
供稿:网友

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

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

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

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

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

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

package.json

 

 
  1. { 
  2. "name": "me2sex-images", 
  3. "version": "0.0.1", 
  4. "description": "Batch download images from http://me2-sex.lofter.com", 
  5. "main": "index.js", 
  6. "author": "Fay", 
  7. "license": "MIT", 
  8. "dependencies": { 
  9. "async": "^0.9.0", 
  10. "cheerio": "^0.18.0", 
  11. "mkdirp": "^0.5.0", 
  12. "request": "^2.51.0", 
  13. "url": "^0.10.2", 
  14. "xml2js": "^0.4.4" 
  15. } 
  16. } 

index.js

 

 
  1. var node = { 
  2. async: require('async'), 
  3. cheerio: require('cheerio'), 
  4. fs: require('fs'), 
  5. mkdirp: require('mkdirp'), 
  6. path: require('path'), 
  7. request: require('request'), 
  8. url: require('url'), 
  9. xml2js: require('xml2js'), 
  10. }; 
  11.  
  12. var Me2SexImages = { 
  13.  
  14. /** 
  15. * 配置选项 
  16. */ 
  17. options: { 
  18. // 网站sitemap地址 
  19. sitemap: 'http://sexy.faceks.com/sitemap.xml', 
  20. // 保存到此文件夹 
  21. saveTo: '/Users/Fay/Pictures/me2sex', 
  22. // 图片并行下载上限 
  23. downLimit: 5, 
  24. }, 
  25.  
  26. posts: [], 
  27.  
  28. /** 
  29. * 开始下载(程序入口函数) 
  30. */ 
  31. start: function() { 
  32. var self = this; 
  33. var async = node.async; 
  34.  
  35. async.waterfall([ 
  36. self.wrapTask(self.sitemapXML), 
  37. self.wrapTask(self.sitemapJSON), 
  38. self.wrapTask(self.downAllImages), 
  39. ], function(err, result) { 
  40. if (err) { 
  41. console.log('error: %s', err.message); 
  42. } else { 
  43. console.log('success: 下载成功'); 
  44. } 
  45. }); 
  46. }, 
  47.  
  48. /** 
  49. * 包裹任务,确保原任务的上下文指向某个特定对象 
  50. * @param {Function} task 符合asycs.js调用方式的任务函数 
  51. * @param {Any} context 上下文 
  52. * @param {Array} exArgs 额外的参数 
  53. * @return {Function} 符合asycs.js调用方式的任务函数 
  54. */ 
  55. wrapTask: function(task, context, exArgs) { 
  56. var self = this; 
  57. return function() { 
  58. var args = [].slice.call(arguments); 
  59. args = exArgs ? exArgs.concat(args) : args; 
  60. task.apply(context || self, args); 
  61. }; 
  62. }, 
  63.  
  64. /** 
  65. * 获取站点sitemap.xml 
  66. */ 
  67. sitemapXML: function(callback) { 
  68. console.log('开始下载sitemap.xml'); 
  69. node.request(this.options.sitemap, function(err, res, body) { 
  70. if (!err) console.log('下载sitemap.xml成功'); 
  71. callback(err, body); 
  72. }); 
  73. }, 
  74.  
  75. /** 
  76. * 将sitemap.xml转成json 
  77. */ 
  78. sitemapJSON: function(sitemapXML, callback) { 
  79. var self = this; 
  80. console.log('开始解析sitemap.xml'); 
  81. node.xml2js.parseString(sitemapXML, {explicitArray: false}, function(err, json) { 
  82. if (!err) { 
  83. self.posts = json.urlset.url; 
  84. self.posts.shift(); 
  85. console.log('解析sitemap.xml成功,共有%d个页面', self.posts.length); 
  86. } 
  87. callback(err, self.posts); 
  88. }); 
  89. }, 
  90.  
  91.  
  92.  
  93. /** 
  94. * 下载整站图片 
  95. */ 
  96. downAllImages: function(callback) { 
  97. var self = this; 
  98. var async = node.async; 
  99. console.log('开始批量下载'); 
  100. async.eachSeries(self.posts, self.wrapTask(self.downPostImages), callback); 
  101. }, 
  102.  
  103.  
  104. /** 
  105. * 下载单个post的图片 
  106. * @param {Object} post 文章 
  107. */ 
  108. downPostImages: function(post, callback) { 
  109. var self = this; 
  110. var async = node.async; 
  111.  
  112. async.waterfall([ 
  113. self.wrapTask(self.mkdir, self, [post]), 
  114. self.wrapTask(self.getPost), 
  115. self.wrapTask(self.parsePost), 
  116. self.wrapTask(self.downImages), 
  117. ], callback); 
  118. }, 
  119.  
  120. mkdir: function(post, callback) { 
  121. var path = node.path; 
  122. var url = node.url.parse(post.loc); 
  123. post.dir = path.join(this.options.saveTo, path.basename(url.pathname)); 
  124.  
  125. console.log('准备创建目录:%s', post.dir); 
  126. if (node.fs.existsSync(post.dir)) { 
  127. callback(null, post); 
  128. console.log('目录:%s 已经存在', post.dir); 
  129. return; 
  130. } 
  131. node.mkdirp(post.dir, function(err) { 
  132. callback(err, post); 
  133. console.log('目录:%s 创建成功', post.dir); 
  134. }); 
  135. }, 
  136.  
  137. /** 
  138. * 获取post内容 
  139. */ 
  140. getPost: function(post, callback) { 
  141. console.log('开始请求页面:%s', post.loc); 
  142. node.request(post.loc, function(err, res, body) { 
  143. if (!err) post.html = body; 
  144. callback(err, post); 
  145. console.log('请求页面成功:%s', post.loc); 
  146. }); 
  147. }, 
  148.  
  149. /** 
  150. * 解析post,并获取post中的图片列表 
  151. */ 
  152. parsePost: function(post, callback) { 
  153. var $ = post.$ = node.cheerio.load(post.html); 
  154. post.images = $('.img') 
  155. .map(function() {return $(this).attr('bigimgsrc');}) 
  156. .toArray(); 
  157. callback(null, post); 
  158. }, 
  159.  
  160. /** 
  161. * 下载post图片列表中的图片 
  162. */ 
  163. downImages: function(post, callback) { 
  164. console.log('发现%d张妹子图片,准备开始下载...', post.images.length); 
  165. node.async.eachLimit( 
  166. post.images, 
  167. this.options.downLimit, 
  168. this.wrapTask(this.downImage, this, [post]), 
  169. callback 
  170. ); 
  171. }, 
  172.  
  173. /** 
  174. * 下载单个图片 
  175. */ 
  176. downImage: function(post, imgsrc, callback) { 
  177. var url = node.url.parse(imgsrc); 
  178. var fileName = node.path.basename(url.pathname); 
  179. var toPath = node.path.join(post.dir, fileName); 
  180. console.log('开始下载图片:%s,保存到:%s,文件名:%s', imgsrc, post.dir, fileName); 
  181. node.request(imgsrc) 
  182. .pipe(node.fs.createWriteStream(toPath)) 
  183. .on('close', function() { 
  184. console.log('图片下载成功:%s', imgsrc); 
  185. callback(); 
  186. }) 
  187. .on('error', callback); 
  188. } 
  189. }; 
  190.  
  191. Me2SexImages.start(); 

以上所述就是本文的全部内容,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选