Node.js abaike图片批量下载Node.js爬虫1.00版
这个与前作的差别在于地址的不规律性,需要找到下一页的地址再爬过去找。
//======================================================
// abaike图片批量下载Node.js爬虫1.00
// 2017年11月9日
//======================================================
// 内置http模块
var http=require("http");
// 内置文件处理模块,用于创建目录和图片文件
var fs=require('fs');
// cheerio模块,提供了类似jQuery的功能,用于从HTML code中查找图片地址和下一页
var cheerio = require("cheerio");
// 请求参数JSON。http和https都有使用
var options;
// request请求
var req;
// 图片数组,找到的图片地址会放到这里
var pictures=[];
//--------------------------------------
// 爬取网页,找图片地址,再爬
// pageUrl sample:http://www.avbaike.net/27812.html
// pageUrl sample:http://www.avbaike.net/27812.html/2
//--------------------------------------
function crawl(pageUrl){
console.log("Current page="+pageUrl);
// 得到hostname和path
var currUrl=pageUrl.replace("http://","");
var pos=currUrl.indexOf("/");
var hostname=currUrl.slice(0,pos);
var path=currUrl.slice(pos);
//console.log("hostname="+hostname);
//console.log("path="+path);
// 初始化options
options={
hostname:hostname,
port:80,
path:path,// 子路径
method:'GET',
};
req=http.request(options,function(resp){
resp.setEncoding('utf8');
var body="";
resp.on('data',function(chunk){
body+=chunk;
});
resp.on('end',function(){
//console.log("body="+body);
var $ = cheerio.load(body);
// 找图片放入数组
$("#post_content p a").each(function(index,element){
var picUrl=$(element).attr("href");
//console.log(picUrl);
pictures.push(picUrl);
})
var nextPageUrl=null;
// 找下一页
$(".pagelist a").each(function(index,element){
var text=$(element).text();
if(text.indexOf('下一页')!=-1){
nextPageUrl=$(element).attr("href");
}
})
if(nextPageUrl==null){
console.log(pageUrl+"已经是最后一页了.");
download(pictures);
}else{
//console.log("下一页是"+nextPageUrl);
crawl(nextPageUrl);
}
});
});
// 超时处理
req.setTimeout(10000,function(){
req.abort();
});
// 出错处理
req.on('error',function(err){
if(err.code=="ECONNRESET"){
console.log('[crawl]socket端口连接超时。');
console.log(err);
}else{
console.log('请求发生错误,err.code:'+err.code);
}
});
// 请求结束
req.end();
}
//--------------------------------------
// 下载图片
//--------------------------------------
function download(pictures){
var folder='pictures';
// 创建目录
fs.mkdir('./'+folder,function(err){
if(err){
console.log("目录"+folder+"已经存在");
}
});
console.log("总计有"+pictures.length+"张图片将被下载.");
for(var i=0;i<pictures.length;i++){
var picUrl=pictures[i];
downloadPic(picUrl);
}
}
//--------------------------------------
// 下载单张图片
// picUrl sample:http://www.avbaike.net/wp-content/uploads/2016/08/108.jpg
//--------------------------------------
function downloadPic(picUrl){
console.log("图片:"+picUrl+"下载开始");
// 得到hostname和path
var currUrl=picUrl.replace("http://","");
var pos=currUrl.indexOf("/");
var hostname=currUrl.slice(0,pos);
var path=currUrl.slice(pos);
//console.log("hostname="+hostname);
//console.log("path="+path);
var picName=currUrl.slice(currUrl.lastIndexOf("/"));
// 初始化options
options={
hostname:hostname,
port:80,
path:path,// 子路径
method:'GET',
};
req=http.request(options,function(resp){
var imgData = "";
resp.setEncoding("binary");
resp.on('data',function(chunk){
imgData+=chunk;
});
resp.on('end',function(){
// 创建文件
var fileName="./pictures"+picName;
fs.writeFile(fileName, imgData, "binary", function(err){
if(err){
console.log("文件"+fileName+"下载失败.");
console.log(err);
}else{
console.log("文件"+fileName+"下载成功");
}
});
});
});
// 超时处理
req.setTimeout(10000,function(){
req.abort();
});
// 出错处理
req.on('error',function(err){
if(err.code=="ECONNRESET"){
console.log('[downloadPic]socket端口连接超时。');
console.log(err);
}else{
console.log('[downloadPic]请求发生错误,err.code:'+err.code);
console.log(err);
}
});
// 请求结束
req.end();
}
//--------------------------------------
// 程序入口
//--------------------------------------
function getInput(){
process.stdout.write("\033[35m 请输入第一页URL:\033[039m"); //紫色
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data',function(text){
process.stdin.end();// 退出输入状态
crawl(text.trim());// trim()是必须的!
});
}
// 调用getInput函数,程序开始
getInput();
Node.js abaike图片批量下载Node.js爬虫1.00版的更多相关文章
- Node.js abaike图片批量下载Node.js爬虫1.01版
//====================================================== // abaike图片批量下载Node.js爬虫1.01 // 1.01 修正了输出目 ...
- Node.js abaike图片批量下载爬虫1.02
//====================================================== // abaike图片批量下载爬虫1.02 // 用最近的断点续传框架改写原有1.01 ...
- Node.js umei图片批量下载Node.js爬虫1.00
这个爬虫在abaike爬虫的基础上改改图片路径和下一页路径就出来了,代码如下: //====================================================== // ...
- Node.js aitaotu图片批量下载Node.js爬虫1.00版
即使是https网页,解析的方式也不是一致的,需要多试试. 代码: //====================================================== // aitaot ...
- Node.js mimimn图片批量下载爬虫 1.00
这个爬虫在Referer设置上和其它爬虫相比有特殊性.代码: //====================================================== // mimimn图片批 ...
- Node.js nvshens图片批量下载爬虫 1.00
//====================================================== // www.nvshens.com图片批量下载Node.js爬虫1.00 // 此程 ...
- Node.js meitulu图片批量下载爬虫1.06版
//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...
- Node.js meitulu图片批量下载爬虫 1.05版(Final最终版)
//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...
- Node.js meitulu图片批量下载爬虫1.04版
//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...
随机推荐
- 关于一些Java基础数据类型的常用方法的应用场景的小思考
昨天遇到一个问题,按照我的一半解决方法是传一个参数,然后通过参数来控制逻辑处理:但是领导发现String的一个方法也可以完全完成该问题!而我完全没有get到这个点! so,我认识到了自己的知识盲区:基 ...
- 【python】python2.x 与 python3.x区别对照+缩进错误解决方法
仅仅列出我用到的,不全. 划重点: 1. urllib2 用 urllib.request 代替 2. urllib.urlencode 用 urllib.parse.urlencode 代替 3. ...
- java的maven项目(一)
Maven项目管理工具 Svn + eclipse 约等于 maven量级 1 Maven的简介 1.1 什么是maven 是apache下的一个开源项目,是纯java开发,并 ...
- 洛谷 P2689 东南西北【模拟/搜索】
题目描述 给出起点和终点的坐标及接下来T个时刻的风向(东南西北),每次可以选择顺风偏移1个单位或者停在原地.求到达终点的最少时间. 如果无法偏移至终点,输出“-1”. 输入输出格式 输入格式: 第一行 ...
- 2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1009
#include<cmath> #include<set> #include<list> #include<deque> #include<map ...
- 奶牛与农夫John与oj
当蒟蒻的我悲惨的发现oj出现大量的奶牛与农夫时,觉得早晚usaco要占领oj,于是绝望的开始记录农夫与奶牛的题目……. 一道usaco月赛的题…在oj用作练习二维数组,虽然数据的大量字符确实很让人不爽 ...
- 加密连接工具Cryptcat
加密连接工具Cryptcat Cryptcat是网络工具Netcat的加密版本.Cryptcat支持TCP.UDP两种网络协议.它可以在两个计算机之间建立指定的连接,并使用特定的密钥对传输数据进行 ...
- AtCoder - 2568 Lotus Leaves
Problem Statement There is a pond with a rectangular shape. The pond is divided into a grid with H r ...
- 【矩阵哈希】【二分答案】【哈希表】bzoj1567 [JSOI2008]Blue Mary的战役地图
引用题解:http://hzwer.com/5153.html 当然,二分可以换成哈希表. #include<cstdio> #include<iostream> #inclu ...
- Bootstrap-table自定义可编辑每页显示记录数
写在前面: 最近在做的person功能,由于后期系统中person人数较多,不利用查找person,故需求方将要求可以自己编辑每页显示的数目,而不是固定的写死每页显示的数目. 下面先来看下bootsr ...