Node.js meitulu图片批量下载爬虫1.02版
以前版本需要先查看网页源码,然后肉眼找到图片数量和子目录,虽说不费事,但多少有点不方便。
于是修改了一下,用cheerio自己去找找到图片数量和子目录,只要修改页面地址就行了。至此社会又前进了一步。
//======================================================
// https://www.meitulu.com图片批量下载Node.js爬虫1.02
// 2017年11月6日
//======================================================
// 内置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;
//--------------------------------------
// 程序入口 Accept-Encoding:gzip, deflate, br
//--------------------------------------
function start(){
var pageUrl="https://www.meitulu.com/item/44.html";// 这版修改这个地址就成
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=0;
var folder="";
// 查找所有class为c_l的节点下面的p节点
$(".c_l p").each(function(index,element){
if(index==2){
var text=$(element).text();
//console.log(text); // 图片数量:112张
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");
//console.log(text); // http://mtl.ttsqgs.com/images/img/44/1.jpg"
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();
}
// 调用start函数,程序开始
start();
Node.js meitulu图片批量下载爬虫1.02版的更多相关文章
- 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. ...
- Node.js meitulu图片批量下载爬虫1.03版
//====================================================== // https://www.meitulu.com图片批量下载Node.js爬虫1. ...
- 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 abaike图片批量下载爬虫1.02
//====================================================== // abaike图片批量下载爬虫1.02 // 用最近的断点续传框架改写原有1.01 ...
- Node.js mm131图片批量下载爬虫1.01 增加断点续传功能
这里的断点续传不是文件下载时的断点续传,而是指在爬行页面时有时会遇到各种网络中断而从中断前的页面及其数据继续爬行的过程,这个过程和断点续传原理上相似故以此命名.我的具体做法是:在下载出现故障或是图片已 ...
- Node.js nvshens图片批量下载爬虫1.01
//====================================================== // nvshens图片批量下载爬虫1.01 // 用最近的断点续传框架改写原有1.0 ...
随机推荐
- 如何在xcode中启用xib方式做应用
使用storybord的好处和缺点这里不一一说,但是很多人可能习惯xib的方式进行应用的开发. 在xcode环境下可参考如下链接进行设定. http://codefromabove.com/2014/ ...
- IOS UITableViewUITableView小技巧--实现cell向左滑动删除,编辑等功能
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return Y ...
- bootstrapValidator关于verbose需要优化的地方
开发中需要用到bootstrapValidator的配置verbose:false,达到当前验证不通过不往下在验证的效果 问题: 当前字段需要remote验证时,此配置无效,原因在于remote是异步 ...
- Spring MVC基础篇4
Spring MVC操作原生Servlet 对象 Spring MVC 可以操作原生的Servlet API,如下的这些原生API,可以各自 自由混合使用,也可以和其他非原生 参数组合使用 实例代码: ...
- 移动APP 支付宝快捷支付开发流程
[代码] [Java]代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- IE添加可信任站点,启用ActiveX插件批处理
添加可信任站点IP地址为:192.168.1.108,启用ActiveX插件执行以下批处理命令: reg add "HKCU\Software\Microsoft\Windows\Curre ...
- HDU 多校1.7
Function Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- Hibernate 配置文件与实体类
今天在配置Hibernate的时候碰到了这样的错误: MappingException: Unknown entity 仔细想了一下,应该是因为我在开始时使用了 Configuration confi ...
- poj3233(等比矩阵求和)
poj3233 题意 给出一个 \(n \times n\) 的矩阵 \(A\) ,求 \(A + A^2 + A^3 + ... + A^k\) . 分析 构造矩阵 \[ \begin{bmatri ...
- CodeForces - 981E Addition on Segments
考虑每个点i在什么情况下会成为最大值. 当选的区间子集是 包含i的区间的一个子集的时候,i肯定会是最大值. 所以我们就可以用这种方法得到所有点的可能的最大值是多少... 也就是说,最后的局面可以仅由一 ...