node.js基础模块http、网页分析工具cherrio实现爬虫
作者:bea
一、前言 说是爬虫初探,其实并没有用到爬虫相关第三方类库,主要用了node.js基础模块http、网页分析工具cherrio。 使用http直接获取url路径对应网页资源,然后使用cherrio分析。 这里我主要学习过的案例自己敲了一遍,加深理解。在coding的过程中,我第一次把jq获取后的对象直接用forEach遍历,直接报错,是因为jq没有对应的这个方法,只有js数组可以调用。 二、知识点 ①:superagent抓去网页工具。我暂时未用到。 ②:cherrio
一、前言 说是爬虫初探,其实并没有用到爬虫相关第三方类库,主要用了node.js基础模块http、网页分析工具cherrio。 使用http直接获取url路径对应网页资源,然后使用cherrio分析。 这里我主要学习过的案例自己敲了一遍,加深理解。在coding的过程中,我第一次把jq获取后的对象直接用forEach遍历,直接报错,是因为jq没有对应的这个方法,只有js数组可以调用。
二、知识点 ①:superagent抓去网页工具。我暂时未用到。 ②:cherrio 网页分析工具,你可以理解其为服务端的jQuery,因为语法都一样。 效果图
1、抓取整个网页
2、分析后的数据,提供的示例为案例实现的例子。
爬虫初探源码分析
var http=require('http');
var cheerio=require('cheerio');
var url='http://www.imooc.com/learn/348';
/****************************
打印得到的数据结构
[{
chapterTitle:'',
videos:[{
title:'',
id:''
}]
}]
********************************/
function printCourseInfo(courseData){
courseData.forEach(function(item){
var chapterTitle=item.chapterTitle;
console.log(chapterTitle+'
');
item.videos.forEach(function(video){
console.log(' 【'+video.id+'】'+video.title+'
');
})
});
}
/*************
分析从网页里抓取到的数据
**************/
function filterChapter(html){
var courseData=[];
var $=cheerio.load(html);
var chapters=$('.chapter');
chapters.each(function(item){
var chapter=$(this);
var chapterTitle=chapter.find('strong').text(); //找到章节标题
var videos=chapter.find('.video').children('li');
var chapterData={
chapterTitle:chapterTitle,
videos:[]
};
videos.each(function(item){
var video=$(this).find('.studyvideo');
var title=video.text();
var id=video.attr('href').split('/video')[1];
chapterData.videos.push({
title:title,
id:id
})
})
courseData.push(chapterData);
});
return courseData;
}
http.get(url,function(res){
var html='';
res.on('data',function(data){
html+=data;
})
res.on('end',function(){
var courseData=filterChapter(html);
printCourseInfo(courseData);
})
}).on('error',function(){
console.log('获取课程数据出错');
})
参考资料: https://github.com/alsotang/node-lessons/tree/master/lesson3
http://www.imooc.com/video/7965
有用 | 无用
二、知识点 ①:superagent抓去网页工具。我暂时未用到。 ②:cherrio 网页分析工具,你可以理解其为服务端的jQuery,因为语法都一样。 效果图
1、抓取整个网页
2、分析后的数据,提供的示例为案例实现的例子。
爬虫初探源码分析
var http=require('http');
var cheerio=require('cheerio');
var url='http://www.imooc.com/learn/348';
/****************************
打印得到的数据结构
[{
chapterTitle:'',
videos:[{
title:'',
id:''
}]
}]
********************************/
function printCourseInfo(courseData){
courseData.forEach(function(item){
var chapterTitle=item.chapterTitle;
console.log(chapterTitle+'
');
item.videos.forEach(function(video){
console.log(' 【'+video.id+'】'+video.title+'
');
})
});
}
/*************
分析从网页里抓取到的数据
**************/
function filterChapter(html){
var courseData=[];
var $=cheerio.load(html);
var chapters=$('.chapter');
chapters.each(function(item){
var chapter=$(this);
var chapterTitle=chapter.find('strong').text(); //找到章节标题
var videos=chapter.find('.video').children('li');
var chapterData={
chapterTitle:chapterTitle,
videos:[]
};
videos.each(function(item){
var video=$(this).find('.studyvideo');
var title=video.text();
var id=video.attr('href').split('/video')[1];
chapterData.videos.push({
title:title,
id:id
})
})
courseData.push(chapterData);
});
return courseData;
}
http.get(url,function(res){
var html='';
res.on('data',function(data){
html+=data;
})
res.on('end',function(){
var courseData=filterChapter(html);
printCourseInfo(courseData);
})
}).on('error',function(){
console.log('获取课程数据出错');
})
参考资料: https://github.com/alsotang/node-lessons/tree/master/lesson3
http://www.imooc.com/video/7965
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JavaScript获取图片像素颜色并转换为box-shadow显示
- 详解Angularjs中的依赖注入
- 详解AngularJS过滤器的使用
- javascript html5 canvas实现可拖动省份的中国地图
- js仿百度登录页实现拖动窗口效果
- 基于jQuery日历插件制作日历
- jQuery Html控件基本操作(日常收集整理)
- JavaScript获取客户端IP的方法(新方法)
- JavaScript高级程序设计(第三版)学习笔记6、7章
- JavaScript高级程序设计(第三版)学习笔记1~5章
- Angularjs中使用Filters详解
- 使用jquery.qrcode.min.js实现中文转化二维码
- JavaScript制作简单的日历效果
- js滑动提示效果代码分享
- js实现页面跳转的五种方法推荐
- js实现页面跳转的五种方法推荐
- js实现上一页下一页的效果【附代码】
- WordPress 单页面上一页下一页的实现方法【附代码】
- JavaScript资源预加载组件和滑屏组件的使用推荐