Node.js meitulu图片批量下载爬虫1.04版
//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1.04 // 1.00 完成图片爬虫,手动输入页数和目录 // 1.01 改写。 // 1.02 手动输入页面url,然后自动解析 // 1.03 从命令行获得页面url,然后自动解析 // 1.04 解决数量节点位置不固定bug和输入状态不退出bug // 2017年11月7日 //====================================================== // 内置https模块,提供了https服务器和客户端功能 var https=require("https"); // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); var zlib = require('zlib'); // cheerio模块,提供了类似jQuery的功能 var cheerio = require("cheerio"); // 内置文件处理模块 var fs=require('fs'); // 请求参数JSON var options; // request请求 var req; //-------------------------------------- // 程序入口 //-------------------------------------- function getInput(){ console.log("请输入页面URL:"); process.stdin.resume(); process.stdin.setEncoding('utf8'); process.stdin.on('data',function(text){ process.stdin.end();// 退出输入状态 start(text.trim());// trim()是必须的! }); //start("https://www.meitulu.com/item/11952.html"); } //-------------------------------------- // 开始下载 //-------------------------------------- function start(pageUrl){ console.log("开始下载..."); var hostName=""; var Path=""; var arr=pageUrl.split("/"); hostName=arr[2]; Path="/"+arr[3]+"/"+arr[4]; // 初始化options options={ hostname:hostName, port:443, path:Path,// 子路径 method:'GET', agent:false, gzip: true, }; req=https.request(options,function(resp){ var html = []; resp.on("data", function(data) { html.push(data); }) resp.on("end", function() { var buffer = Buffer.concat(html); zlib.gunzip(buffer, function(err, decoded) { //console.log(decoded.toString());// gzip解压后的html文本 var body=decoded.toString(); var $ = cheerio.load(body); var endIndex=-1; var folder=""; // 查找所有class为c_l的节点下面的p节点 $(".c_l p").each(function(index,element){ var text=$(element).text(); if(text.indexOf("数量")!=-1 && endIndex==-1){ var arr=text.split(" "); endIndex=arr[1]; } }) // 查找所有class为c_l的节点下面的p节点 $(".content center img").each(function(index,element){ if(index==0){ var text=$(element).attr("src"); var arr=text.split("/"); folder=arr[arr.length-2]; } }) console.log(endIndex); console.log(folder); fs.mkdir('./'+folder,function(err){ if(err){ console.log("目录"+folder+"已经存在"); } }); // 下载图片 for(var i=1;i<=endIndex;i++){ downloadPic(folder,i); } }) }).on("error", function() { console.log("获取失败") }) }); // 超时处理 req.setTimeout(5000,function(){ req.abort(); }); // 出错处理 req.on('error',function(err){ if(err.code=="ECONNRESET"){ console.log('socket端口连接超时。'); }else{ console.log('请求发生错误,err.code:'+err.code); } }); // 请求结束 req.end(); } //-------------------------------------- // 下载图片 // folder:图片所在url的目录 // pinctureIndex:图片序号 //-------------------------------------- function downloadPic(folder,pinctureIndex){ console.log("开始下载"+pinctureIndex); // 初始化options options={ hostname:'mtl.ttsqgs.com',// 这里别加http://,否则会出现ENOTFOUND错误 port:80, path:'/images/img/'+folder+'/'+pinctureIndex+'.jpg',// 子路径 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="./"+folder+"/"+pinctureIndex+".jpg"; fs.writeFile(fileName, imgData, "binary", function(err){ if(err){ console.log("文件"+fileName+"下载失败."); } console.log(fileName+"下载成功"); }); }); }); // 超时处理 req.setTimeout(5000,function(){ req.abort(); }); // 出错处理 req.on('error',function(err){ if(err.code=="ECONNRESET"){ console.log('socket端口连接超时。'); }else{ console.log('请求发生错误,err.code:'+err.code); } }); // 请求结束 req.end(); } // 调用getInput函数,程序开始 getInput();
2017年11月7日05:05:38
Node.js meitulu图片批量下载爬虫1.04版的更多相关文章
- 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.03版
//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...
- Node.js meitulu图片批量下载爬虫1.02版
以前版本需要先查看网页源码,然后肉眼找到图片数量和子目录,虽说不费事,但多少有点不方便. 于是修改了一下,用cheerio自己去找找到图片数量和子目录,只要修改页面地址就行了.至此社会又前进了一步. ...
- Node.js meitulu图片批量下载爬虫1.01版
在 http://www.cnblogs.com/xiandedanteng/p/7614051.html 一文我曾经书写过一个图片下载爬虫,但原有程序不是为下载图片而设计故有些绕,于是稍微改写了一下 ...
- Node.js meitulu图片批量下载爬虫1.051
原有1.05版程序没有断点续传模式,现在在最近程序基础上改写一版1.051. //====================================================== // m ...
- Node.js mm131图片批量下载爬虫1.01 增加断点续传功能
这里的断点续传不是文件下载时的断点续传,而是指在爬行页面时有时会遇到各种网络中断而从中断前的页面及其数据继续爬行的过程,这个过程和断点续传原理上相似故以此命名.我的具体做法是:在下载出现故障或是图片已 ...
- Node.js abaike图片批量下载爬虫1.02
//====================================================== // abaike图片批量下载爬虫1.02 // 用最近的断点续传框架改写原有1.01 ...
- Node.js nvshens图片批量下载爬虫1.01
//====================================================== // nvshens图片批量下载爬虫1.01 // 用最近的断点续传框架改写原有1.0 ...
随机推荐
- golang的各种打印
golang的打印方法太特么多了,下面就来区分一下之间的差异 package main import ( "fmt" "os" ) func main() { ...
- 分析函数调用堆栈的原理和Delphi实现
来自:http://blog.163.com/liuguang_123/blog/static/816701920105262543890/ ----------------------------- ...
- tcpdump 学习(3):MySQL Query
在MySQL线上环境我们一般只打开了binary log,slow log,有时我们需要查看general log呢?因为该log记录所有的请求,打开该日志肯定给磁盘造成很大压力,IO能力有所下降,所 ...
- 解决windows下文件拷贝到ubuntu下文件名乱码的问题
sudo apt-get install convmv 解压zip文件: convmv -f gbk -t utf8 -r --notest *
- SDL安装小结
SDL是一个基于C的简易实现,安装过程中也多亏了,各位大神的助攻,这里简单mark一下遇到的问题,以备查找: 关于VS的版本:目前文档里确定支持的VS为2008到2013,我的VS是2013,2015 ...
- oracle tablespace usage status
select a.tablespace_name, a.bytes / 1024 / 1024 "Sum MB", (a.bytes - b.bytes) / 1024 / 102 ...
- centos 7 下发送邮件设置
使用sendmail发邮件的配置 yum -y install sendmailservice sendmail start vim body.txttest mail from linux. mai ...
- intellij idea android错误: Missing styles. Is the correct theme chosen for this layout?
Missing styles. Is the correct theme chosen for this layout? Use the Theme combo box above the layou ...
- 【进制转换】codevs 1474 十进制转m进制
#include<cstdio> using namespace std; ],en; int main() { scanf("%d%d",&n,&m) ...
- 【费马小定理+矩阵快速幂】HDU4549——M斐波那契数列
[题目大意] M斐波那契数列F[n]是一种整数数列,它的定义如下:F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 )现在给出a, b, n,求出F[ ...