当前位置: 首页 > 后端技术 > Node.js

node爬虫快速介绍

时间:2023-04-04 00:43:04 Node.js

node爬虫是前端新手,刚接触node。他们对耳闻已久的节点爬虫非常着迷,所以有了这篇文章,项目代码需要在文章信息末尾抓取天涯论坛重庆版块的文章列表。使用工具node.jssuperagent(客户端请求代理模块)cheerio(一个快速、灵活、可实现的jQuery核心实现,专门为服务端定制)安装使用cheerio,superagent模块安装npminstallsuperagentcheerio--save中引入cheerioproject,superagentconstsuperagent=require('superagent')constcheerio=require('cheerio')指定要爬取的域名constmainUrl='http://bbs.tianya.cn'//天涯论坛主域名leturl='/list-45-1.shtml'//重庆地区域名请求数据superagent.get(mainUrl+url).end(function(err,res){//throw拦截if(err){returnthrowError(err)}console.log(res)}分析页面结构分析页面内容,提取出我们需要的内容,每一列信息都在('.mt5tabletbodytr')下。调用cheerio选择('.mt5tabletbodytr')let$=cheerio.load(res.text)$('.mt5tabletbodytr').each((index,item)=>{//这里是eachitem的信息})找到信息,然后解析找到的信息和解析数据,找到需要解析的数据,解析数据,保存我们需要的数据let$=cheerio.load(res.text)letdata=[]//存储抓取的数据$('.mt5tabletbodytr').each((index,item)=>{let_this=$(item)//根据判断是否为文章到页面if($(_this.children()[0]).hasClass('td-title')){//存储数据letobjlettitle=$(_this.find('.td-title')).find('span').next().text()//lettext=$(_this.find('a')[0]).text()//另一个选择器lettype=$(_this.find('.td-title')).find('.face').attr('title')让goto=$(_this.find('.td-title')).find('span').next().attr('href')letauthor=$(_this.children()[1]).text()letpoint=$(_this.children()[2]).text()lettime=$(_this.children()[3]).text()obj={title:title,type:type,url:mainUrl+goto,author:author,point:point,time:time}if(obj.title!=""){//判断是否有内容,推送todata.push(obj)indata}}})要在本地存储数据,需要将data中保存的数据保存到你要保存的文件中。需要用到节点的fs模块1.引入fs模块constfs=require('fs')2.本地存储数据在根目录下创建data文件夹fs.writeFile(__dirname+'/data/articleLists.json',JSON.stringify({status:0,data:data}),function(err){if(err){console.log(err)}else{console.log("文章列表写入完成")}})现在爬虫会将爬取到的数据存储到本地ok了,到这里爬虫已经完成了,接下来我们需要优化一下,让爬虫更聪明现在我们的爬虫只能爬取当前页面的信息,我们改成这样说它还可以翻页,分析一下翻页按钮,天涯论坛的列表下一页按钮里面也有一个标签,里面的url加上我们之前记录的mainUrl就是下一页的标签所以,当爬虫爬完这个页面的数据后,让爬虫向下一页的链接发送新的请求,继续爬取。//单次读取后,找到下一页的链接,继续抓取下一页的数据letnextPage=$('.mt5').next().find('.short-pages-2.links')nextPage.children().each((index,item)=>{if($(item).text()==='下一页'){leturl=$(item).attr("href")getData(url)//我们刚才用来请求数据的方法,命名为这个函数}})现在,爬虫读取完当前页面的数据后,会继续爬取下一页的数据。完成代码后,我还加了页码,每页数据单独记录。以下为完整代码constsuperagent=require('superagent')constcheerio=require('cheerio')constfs=require('fs')constmainUrl='http://bbs.tianya.cn'//天涯论坛主域名leturl='/list-45-1.shtml'//重庆区域域名letindex=1//记录页码//发送请求获取页面资源方法letgetData=(url)=>{//使用superagent请求页面数据superagent.get(mainUrl+url).end(function(err,res){//抛出错误拦截if(err){returnthrowError(err)}//之后使用cheerio解析数据请求数据let$=cheerio.load(res.text)letdata=[]//存储捕获的数据$('.mt5tabletbodytr').each((index,item)=>{let_this=$(item)//根据页面判断是否为文章if($(_this.children()[0]).hasClass('td-title')){//存储数据letobjlettitle=$(_this.find('.td-title')).find('span').next().text()//让text=$(_this.find('a')[0]).text()//另一个选项lettype=$(_this.find('.td-title')).find('.face').attr('title')让goto=$(_this.find('.td-title')).find('span').next().attr('href')让author=$(_this.children()[1]).text()letpoint=$(_this.children()[2]).text()lettime=$(_this.children()[3]).text()obj={title:title,type:type,url:mainUrl+goto,author:author,point:point,time:time}if(obj.title!=""){//判断是否有内容,然后推送到数据data.push(obj)}}})if(data.length>0){//判断data中是否有内容//使用fs模块将数据存入data中,或者使用数据库操作fs.writeFile(__dirname+'/data/articleLists'+index+'.json',JSON.stringify({status:0,data:data}),function(err){if(err){console.log(err)}else{console.log("写完文章列表,当前页码:",index)index++}})}//单次读取后,找到下一页链接,继续抓取下一页的数据letnextPage=$('.mt5').next().find('.short-pages-2.links')nextPage.children().each((index,item)=>{if($(item).text()==='下一页'){leturl=$(item).attr("href")getData(url)}})})}//这个本篇节点爬虫快速入门文章到此结束,但是本爬虫还有很多地方需要完善,后续会为大家带来更详细的爬虫教程