首页 > 语言 > JavaScript > 正文

nodejs通过phantomjs实现下载网页

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

这篇文章主要介绍了nodejs通过phantomjs实现下载网页的方法,有需要的小伙伴可以参考下。

功能其实很见简单,通过 phantomjs.exe 采集 url 加载的资源,通过子进程的方式,启动nodejs 加载所有的资源,对于css的资源,匹配css内容,下载里面的url资源

当然功能还是很简单的,在响应式设计和异步加载的情况下,还是有很多资源没有能够下载,需要根据实际情况处理下

首先当然是下载 nodejs 和 phantomjs

下面是 phantomjs.exe 执行的 down.js

 

 
  1. var page = require('webpage').create(), 
  2. system = require('system'); 
  3. var spawn = require("child_process").spawn 
  4.  
  5. if (system.args.length === 1) { 
  6. console.log('Usage: netsniff.js <some URL>'); 
  7. phantom.exit(1); 
  8. } else { 
  9. var urls = []; 
  10. page.address = system.args[1]; 
  11. page.onResourceReceived = function (res) { 
  12. if (res.stage === 'start') { 
  13. urls.push(res.url); 
  14. } 
  15. }; 
  16. page.open(page.address, function (status) { 
  17. var har; 
  18. if (status !== 'success') { 
  19. console.log('FAIL to load the address'); 
  20. phantom.exit(1); 
  21. } else { 
  22. console.log('down resource ' + urls.length + ' urls.'); 
  23. var child = spawn("node", ["--harmony", "downHtml.js", urls.join(',')]) 
  24. child.stdout.on("data", function (data) { 
  25. console.log(data); 
  26. }) 
  27. child.stderr.on("data", function (data) { 
  28. console.log(data); 
  29. }) 
  30. child.on("exit", function (code) { 
  31. phantom.exit(); 
  32. })  
  33. } 
  34. }); 
  35. } 

下面是对应的node运行的 downHtml.js

 

 
  1. "use strict"; 
  2. var fs = require('fs'); 
  3. var http = require('http'); 
  4. var path = require('path'); 
  5. var r_url = require('url'); 
  6.  
  7. var dirCache = {};//缓存减少判断 
  8. function makedir (pathStr, callback) { 
  9. if (dirCache[pathStr] == 1) { 
  10. callback(); 
  11. } else { 
  12. fs.exists(pathStr, function (exists) { 
  13. if (exists == true) { 
  14. dirCache[pathStr] == 1; 
  15. callback(); 
  16. } else { 
  17. makedir(path.dirname(pathStr), function () { 
  18. fs.mkdir(pathStr, function () { 
  19. dirCache[pathStr] == 1; 
  20. callback(); 
  21. }) 
  22. }); 
  23. } 
  24. }) 
  25. } 
  26. }; 
  27.  
  28. var reg = /[:,]/s*url/(['"]?.*?(/1)/)/g 
  29. var reg2 = //((['"]?)(.*?)(/1)/)/ 
  30. var isDownMap = {}; 
  31. var downImgFromCss = function (URL) { 
  32. http.get(URL, function(res) { 
  33. //console.log(path.resolve(process.cwd(), 'index.min.css')) 
  34. //res.pipe(fs.createWriteStream(path.resolve(process.cwd(), 'index.min.css'))); 
  35. var body = ""; 
  36. res.setEncoding('utf8'); 
  37. res.on('data', function (chunk) { 
  38. body += chunk; 
  39. }); 
  40. res.on('end', function () { 
  41. var match = body.match(reg); 
  42. for (var i = 0, len = match.length; i < len; i++){ 
  43. var m = match[i].match(reg2); 
  44. if (m && m[2]) { 
  45. var url = m[2]; 
  46. let imgUrl = r_url.resolve(URL, url); 
  47. if (!isDownMap[imgUrl]) { 
  48. var uo = r_url.parse(imgUrl); 
  49. let filepath = CWD + '/' + uo.hostname + uo.pathname; 
  50. makedir(path.dirname(filepath), function () { 
  51. http.get(imgUrl, function (res) { 
  52. res.pipe(fs.createWriteStream(filepath)); 
  53. }) 
  54. }) 
  55. isDownMap[imgUrl] = 1; 
  56. } 
  57. } 
  58. } 
  59. }); 
  60. }); 
  61. } 
  62.  
  63. var URLS = process.argv[2].split(','); 
  64. var CWD = process.cwd(); 
  65. //下载资源 
  66. URLS.forEach(function (URL) { 
  67. var uo = r_url.parse(URL); 
  68. var filepath; 
  69. if (uo.pathname == '/' || uo.pathname == '') { 
  70. filepath = CWD + '/' + uo.hostname + '/index.html'; 
  71. } else { 
  72. filepath = CWD + '/' + uo.hostname + uo.pathname; 
  73. } 
  74. makedir(path.dirname(filepath), function () { 
  75. http.get(URL, function (res) { 
  76. if (URL.indexOf('.css') != -1 || (res.headers["content-type"] && res.headers["content-type"].indexOf('text/css')!= -1)) { 
  77. console.log('down images form css file:' + URL + '.'); 
  78. downImgFromCss(URL); 
  79. } 
  80. res.pipe(fs.createWriteStream(filepath)); 
  81. }) 
  82. }); 
  83. }); 

down.js downHtml.js 放在同一个文件夹下 通过下列 cmd 运行

D:/phantomjs-2.0.0-windows/bin/phantomjs.exe down.jshttp://www.youku.com/

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

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

图片精选