• node 爬虫 --- 批量下载图片


    步骤一:创建项目

    npm init

    步骤二:安装 request,cheerio,async 三个模块 

    request 
    用于请求地址和快速下载图片流。 
    https://github.com/request/request 
    cheerio 
    为服务器特别定制的,快速、灵活、实施的jQuery核心实现. 
    便于解析html代码。 
    https://www.npmjs.com/package/cheerio 
    async 
    异步调用,防止堵塞。 
    http://caolan.github.io/async/

    npm i request cheerio async -D

    步骤三:核心思路

    (1)用request 发送一个请求。获取html代码,取得其中的img标签和a标签。

    //发送请求
    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) {
            // 请求成功
          }
        }
      });
    }

    (2)通过获取的a表情进行递归调用。不断获取img地址和a地址,继续递归

    // 递归爬虫
    $('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;
      }
    })

    (3)获取img地址通过request(photo).pipe(fs.createWriteStream(dir + “/” + filename));进行快速下载。

    //发送请求
    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 == '.gif' || 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;
              }
            })
          }
        }
      });
    }

    步骤四:防坑

    (1)在request通过图片地址下载时,绑定error事件防止爬虫异常的中断。

    // 防止pipe错误
    request(photo)
      .on('error', function (err) {
        console.log(err);
      })
      .pipe(fs.createWriteStream(dir + "/" + filename));

    (2)通过async的mapLimit限制并发。 

    async.mapLimit(photos, asyncNum, function (photo, callback) {
      // 请求成功
    }, function (err, result) {
      // 请求失败
    })

    (3)加入请求报头,防止ip被屏蔽。 

    setting.js

    /**
     * 加入请求报头,防止ip被屏蔽
     */
    module.exports = {
      header : {
        'User-Agent': 'request',
        "Referer":"http://www.ivsky.com/",
      },
      // 并发数
      ajax_timeout : 2000,
      // 下载图片速度
      download_v : 5
    }

    (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);
        }
      })
    }

    步骤五:完整代码

    app.js

    /**
     * node 爬虫
     */
    var fs = require('fs');
    var request = require("request");
    var cheerio = require("cheerio");
    var async = require('async');
    
    // 目标网址
    var url = 'http://www.ivsky.com/tupian/ziranfengguang/';
    
    // 本地存储目录
    var dir = './images';
    
    var setting = require('./setting');
    
    var timeout = 100;
    // 封装了一层函数
    function fetchre(url) {
      requestall(url);
    }
    // 发送请求
    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 == '.gif' || 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;
              }
            })
          }
        }
      });
    }
    
    // 下载图片
    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);
        }
      })
    }
    
    // 判断是否为完整地址
    function IsURL(str_url) {
      var strRegex = '^((https|http|ftp|rtsp|mms)?://)';
      var re = new RegExp(strRegex);
      if (re.test(str_url)) {
        return (true);
      } else {
        return (false);
      }
    }
    
    requestall(url);

    步骤六:执行

    (1)在根路径下创建 images 空文件夹,用于存放下载下来的图片

    (2)打开终端,执行

    node app.js

    (3)项目目录

  • 相关阅读:
    HTML页面保存为图片
    一些chrome调试
    hooks使用的一些注意点
    React(v16.8) Hooks 简析
    基于 React.js 和 Node.js 的 SSR 实现方案
    移动端垂直居中对齐
    python XML ElementTree的增删改查
    python实现XML解析的三种方法
    git的用法
    Appium获取元素的方式
  • 原文地址:https://www.cnblogs.com/crazycode2/p/8193474.html
Copyright © 2020-2023  润新知