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 ...
随机推荐
- 原型 Boolean String Math Date知识点
原型 1.定义 每一个对象都有原型 原型仍然是一个对象 模拟实现面向对象的继承性 2.原型链 对象的原型还有原型 对象除了可以使用自有属性还可以继承原型上的属性 3.获取原型 对象.__proto__ ...
- 【转】spring 装配Bean中构造参数的注入
转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...
- [ Mongodb ] 问题总汇
1. Mongodb备份 [root@localhost ~]# mongodump -h /users 2. Mongodb恢复 [root@localhost ~]# mongorestore - ...
- CentOS7配置阿里云yum源和EPEL源
配置阿里云yum源(参考:http://mirrors.aliyun.com/help/centos) 1.备份 [root@bogon ~]# cd /etc/yum.repos.d/ [root@ ...
- Django组件之contenttype的应用
contenttypes 是Django内置的一个应用,可以追踪项目中所有app和model的对应关系,并记录在ContentType表中. 每当我们创建了新的model并执行数据库迁移后,Conte ...
- 关于iBatis-selectKey的一点笔记
技术前提:我们使用iBatis作为持久层方案 技术场景: 假设我们有两张表,一张主表Main,一张子表Sub,并且主表的主键是由数据库维护的自增长的主键,子表中有一个字段引用这个主键,那么当我 ...
- XAMPP配置vhosts多站点/绝对正确
XAMPP有时候你需要一些顶级域名访问方式来访问你本地的项目也就是虚拟主机配置,这时候就需要配置虚拟主机,给你的目录绑定一个域名,实现多域名绑定访问. 在Mac 下一直使用 MAMP 搭建本地 php ...
- POJ 1321 棋盘问题【DFS/回溯/放与不放/类似n皇后】
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62164 Accepted: 29754 Description 在一 ...
- SPOJ UOFTCG - Office Mates (树的最小路径覆盖)
UOFTCG - Office Mates no tags Dr. Baws has an interesting problem. His N graduate students, while f ...
- HDU1142 A Walk Through the Forest(dijkstra)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...