步骤一:创建项目

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)项目目录

最新文章

  1. linux Mint wine安装qq,桌面快捷键配置
  2. HTTP 错误500.19 -Internal Server Error 错误代码 0x80070021
  3. SEO 百度后台主动推送链接
  4. HTK学习2:工具使用
  5. vim 7.4 编译安装
  6. shutdown -s -t
  7. Ossec常用命令
  8. v8 源码获取与build
  9. Gradient Descent 和 Stochastic Gradient Descent(随机梯度下降法)
  10. 使用Marshal.Copy把Txt行数据转为Struct类型值
  11. OC对象创建过程
  12. 2014年辛星全然解读html第八节
  13. kotlin成长之路
  14. 常用Oracle进程资源查询语句(运维必看)
  15. Android开源项目——设置图文居中的按钮 IconButton
  16. hibernate关系映射
  17. amd,cmd规范
  18. 小学四则运算APP 第一个冲刺 第七天
  19. git web开发版本管理
  20. 弹出层小插件之(一)sweetalert

热门文章

  1. Python9-面对对象2-day23
  2. Python9-面对对象1-day22
  3. linux c中需要记住的东西
  4. appium+python自动化-adb logcat查看日志
  5. 信安实验-RC4加密算法
  6. Python之Monitor监控线程(干货)
  7. 【Luogu】P3391文艺平衡树(Splay)
  8. 算法复习——迭代加深搜索(骑士精神bzoj1085)
  9. Hibernate 笔记 HQL查询 条件查询,聚集函数,子查询,导航查询
  10. 史上最详细的linux关于connect: network is unreachable 问题的解决方案