源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

基于nodejs 的多页面爬虫实例代码

  • 时间:2022-02-15 04:37 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:基于nodejs 的多页面爬虫实例代码
[b]前言[/b] 前端时间再回顾了一下node.js,于是顺势做了一个爬虫来加深自己对node的理解。 主要用的到是request,cheerio,async三个模块 [b]request [/b] 用于请求地址和快速下载图片流。 [url=https://github.com/request/request]https://github.com/request/request [/url] [b]cheerio [/b] 为服务器特别定制的,快速、灵活、实施的jQuery核心实现. 便于解析html代码。 [url=https://www.npmjs.com/package/cheerio]https://www.npmjs.com/package/cheerio [/url] [b]async [/b] 异步调用,防止堵塞。 [url=http://caolan.github.io/async/]http://caolan.github.io/async/[/url] [b]核心思路[/b] [list=1] [*]用request 发送一个请求。获取html代码,取得其中的img标签和a标签。[/*] [*]通过获取的a表情进行递归调用。不断获取img地址和a地址,继续递归[/*] [*]获取img地址通过request(photo).pipe(fs.createWriteStream(dir + “/” + filename));进行快速下载。[/*] [/list]
function requestall(url) {

 request({

  uri: url,

  headers: setting.header

 }, function (error, response, body) {

  if (error) {

   console.log(error);

  } else {

   console.log(response.statusCode);

   if (!error && response.statusCode == 200) {

    var $ = cheerio.load(body);

    var photos = [];

    $('img').each(function () {

     // 判断地址是否存在

     if ($(this).attr('src')) {

      var src = $(this).attr('src');

      var end = src.substr(-4, 4).toLowerCase();

      if (end == '.jpg' || end == '.png' || end == '.jpeg') {

       if (IsURL(src)) {

        photos.push(src);

       }

      }

     }

    });

    downloadImg(photos, dir, setting.download_v);

    // 递归爬虫

    $('a').each(function () {

     var murl = $(this).attr('href');

     if (IsURL(murl)) {

      setTimeout(function () {

       fetchre(murl);

      }, timeout);

      timeout += setting.ajax_timeout;

     } else {

      setTimeout(function () {

       fetchre("http://www.ivsky.com/" + murl);

      }, timeout);

      timeout += setting.ajax_timeout;

     }

    })

   }

  }

 });

} 
[b]防坑[/b] 1.在request通过图片地址下载时,绑定error事件防止爬虫异常的中断。 2.通过async的mapLimit限制并发。 3.加入请求报头,防止ip被屏蔽。 4.获取一些图片和超链接地址,可能是相对路径(待考虑解决是否有通过方法)。
function downloadImg(photos, dir, asyncNum) {

 console.log("即将异步并发下载图片,当前并发数为:" + asyncNum);

 async.mapLimit(photos, asyncNum, function (photo, callback) {

  var filename = (new Date().getTime()) + photo.substr(-4, 4);

  if (filename) {

   console.log('正在下载' + photo);

   // 默认

   // fs.createWriteStream(dir + "/" + filename)

   // 防止pipe错误

   request(photo)

    .on('error', function (err) {

     console.log(err);

    })

    .pipe(fs.createWriteStream(dir + "/" + filename));

   console.log('下载完成');

   callback(null, filename);

  }

 }, function (err, result) {

  if (err) {

   console.log(err);

  } else {

   console.log(" all right ! ");

   console.log(result);

  }

 })

} 
[b]测试:[/b] [img]http://files.jb51.net/file_images/article/201705/201705310914097.png[/img] 可以感觉到速度还是比较快的。 [img]http://files.jb51.net/file_images/article/201705/201705310914108.png[/img]   完整地址。[url=https://github.com/hua1995116/node-crawler/]https://github.com/hua1995116/node-crawler/[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部