nodejs抓取图片案例

const superagent = require(‘superagent’);
const cheerio = require(‘cheerio’);
const async = require(‘async’);
const fs = require(‘fs’);
const url = require(‘url’);
const request =require(‘request’);
const hupuUrl = ‘https://bbs.hupu.com/selfie-1’;

let ssr = [];
let allUrl = [];
let curCount = 0;

for (let i = 1; i <= 4; i++) {
hupuUrl2 = ‘https://bbs.hupu.com/selfie-’ + i;
//for循环把五页的页面循环出来
superagent.get(hupuUrl2)
//通过superagent去请求每一页
.end(function (err, res) {
if (err) {
return console.error(err);
}
//cheerio nodejs版的JQ
let $ = cheerio.load(res.text);
//获取首页所有的链接
$(’.titlelink>a:first-child’).each(function (idx, element) {
let $element = $(element);
let href = url.resolve(hupuUrl2, $element.attr(‘href’));
allUrl.push(href);
curCount++;
//获取到此url,异步进行以下操作,此操作为进入到这个帖子中爬取数据
superagent.get(href)
.end(function (err, res) {
if(err){
return console.error(err);
}
let $ = cheerio.load(res.text);
let add = href;
let title = $(’.bbs-hd-h1>h1’).attr(‘data-title’);//帖子标题
let tximg = $(’.headpic:first-child>img’).attr(‘src’);//用户头像
let txname = $(’.j_u:first-child’).attr(‘uname’);//用户ID
let contentimg1 = $(’.quote-content>p:nth-child(1)>img’).attr(‘src’);//爆照图1
let contentimg2 = $(’.quote-content>p:nth-child(2)>img’).attr(‘src’);//爆照图2
let contentimg3 = $(’.quote-content>p:nth-child(3)>img’).attr(‘src’);//爆照图3
ssr.push({
‘tx’: tximg,
‘name’: txname,
‘pic’: contentimg1,contentimg2,contentimg3
});
//把数据存储到一个对象里
let stad = {
“address”: add,
“title”:title,
“ID” : txname,
“touxiang” : tximg,
“pic1” : contentimg1,
“pic2” : contentimg2,
“pic3” : contentimg3
};
let picArr = [contentimg1,contentimg2,contentimg3];
//console.log(stad);
//通过fs模块把数据写入本地json
fs.appendFile(‘data/result1.json’, JSON.stringify(stad) ,‘utf-8’, function (err) {
if(err) throw new Error(“appendFile failed…”);
//console.log(“数据写入success…”);
});
//定义一个以title为文件夹名的路径,作为以后下载图片时使用
let lujin = ‘data/’ + title + ‘/’;
//判断文件夹是否存在
fs.exists(‘data/111’,function (exists) {
if(!exists){
fs.mkdir(“data/111”, function(err) {
// if (err) {
// throw err;
// }
});
console.log(‘ye’)
}else {
async.mapSeries(picArr,function(item, callback){
setTimeout(function(){
//downloadPic方法下载图片
if(item === undefined) {
console.log(“undefined”)
}else {
console.log(“下载图片:”+item)
request(item).pipe(fs.createWriteStream(‘data/111/’+ (new Date()).getTime() +’.jpg’)).on(‘close’,function(){
console.log(‘pic saved!’)
})
}

// downloadPic(item, ‘data/’+ (new Date()).getTime() +’.jpg’);
callback(null, item);
},400);
}, function(err, results){});
console.log(‘er’)
}
})
})
});
});
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值