本文实例为大家分享了nodejs爬虫获取数据代码,供大家参考,具体内容如下 var Http=require('http'); var cheerio=require('cheerio');//页面获取
本文实例为大家分享了nodejs爬虫获取数据代码,供大家参考,具体内容如下
var Http=require('http');
var cheerio=require('cheerio');//页面获取到的数据模块
var url='http://www.jcpeixun.com/lesson/1512/';
function filterData(html){
var $=cheerio.load(html);
var courseData=[];
var chapters=$(".list-collapse");
chapters.each(function(item){
var chapterTitle=$(this).find(".collapse-head").find("label").text();
var videos=$(this).find(".listview5").children("li");
var chaptersData={
chaptersTitle:chapterTitle,
videosData:[]
}
videos.each(function(item){
var videoTitle=$(this).find(".ml10").attr('data-lesson-name');
var videoId=$(this).find(".ml10").attr('data-lesson-id');
var vadeoPrice=$(this).find(".colblue").text();
chaptersData.videosData.push({
title:videoTitle,
id:videoId,
price:vadeoPrice
})
})
courseData.push(chaptersData)
})
return courseData
}
function printCourseInfo(courseData){
courseData.forEach(function(item){
console.log(item.chaptersTitle+'n');
item.videosData.forEach(function(item){
console.log(item.title+'【'+item.id+'】'+item.price+'n')
})
})
}
http.get(url,function(res){
html="";
res.on("data",function(data){
html+=data
})
res.on('end',function(){
var courseData=filterData(html);
printCourseInfo(courseData)
})
})
效果图:
--结束END--
本文标题: nodeJs爬虫获取数据简单实现代码
本文链接: https://www.lsjlt.com/news/13019.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2022-06-04
2022-06-04
2022-06-04
2022-06-04
2022-06-04
2022-06-04
2022-06-04
2022-06-04
2022-06-04
2022-06-04
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0