分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

Node.js abaike图片批量下载Node.js爬虫1.00版

发布时间:2023-09-06 01:24责任编辑:郭大石关键词:jsNode爬虫

这个与前作的差别在于地址的不规律性,需要找到下一页的地址再爬过去找。

//======================================================// 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版

原文地址:http://www.cnblogs.com/xiandedanteng/p/7816803.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved