Cheerio是 Node.js 特别为服务端定制的,能够快速灵活的对 JQuery 核心进行实现。它工作于 DOM模型
上,且解析、操作、呈送都很高效,适合各种Web爬虫程序。 数据源
爬虫要想爬取数据首先提供爬取数据的路径:
url: http://www.hubwiz.com/course/562427361bc20c980538e26fhttp://xc.hubwiz.com/course/562427361bc20c980538e26f
爬虫目标
页面每一章节的标题及其中小节的标题名称。
小爬虫
首先引用了 nodejs 的核心模块 http
和提供了爬取路径,然后通过 http
中的 get
接口给 url
发送 get
请求,最回调函数中对请求回来的数据进行处理。
爬取数据
通过cheerio的 load 方法把html加载;然后对.panel
通过 map
进行遍历。之后我们在 map 中 组装 要数据格式,如上述中chapterData。再对 小节 li 进行遍历,把 section
通过 push
方法 添加到 chapterData.section 的数组中。再把组装好的数据 push 到 我们创建的空数组 data 中。最后通过console.log进行输出。
处理数据
在大多数情况下我们爬取出来的数据,可能不是我们最终想要的东西比如说:数据中空值或者空格等等。在获取在通过 text 获取内容的后面跟随着一个 trim
的方法。这个方法的作用就是处理数据中空格和换行符。
空值的情况,在输出的数据中存在一个空的数组对象,通过 filter 方法去处理它 。
输出数据
在 crawlerChapter
方法中得到的数据 data 组装,进行输出。
在 printInfo
方法中的参数 data ,这个参数需要 crawlerChapter
方法 return
给 printInfo
。然后就是 data 参数调用 filter 方法把数据为空的去掉。最后就是把章节拼接字符串进行输出。
参考源码:
var http = require('http');
var cheerio = require('cheerio');
var url = 'http://www.hubwiz.com/course/5437538a032c781670afddbe/';
http.get(url, function (res) {
var html = '';
res.on('data', function (data) {
html += data;
});
res.on('end', function () {
var chapter = crawlerChapter(html);
printInfo(chapter);
})
}).on('error', function () {
console.log('爬取页面错误')
});
function crawlerChapter(html) {
var $ = cheerio.load(html);
var chapters = $('.panel');
var data = [];
chapters.map(function (node) {
var chapters = $(this);
var chapterTitle = chapters.find('h4').text().trim();
var sections = chapters.find('li');
var chapterData = {
chaptersTitle: chapterTitle,
section: []
};
sections.map(function (node) {
var section = $(this).text().trim();
chapterData.section.push(section);
});
data.push(chapterData);
});
return data;
}
function printInfo(data) {
data = data.filter(function filterByID(obj) {
return obj.chaptersTitle ? true : false;
});
data.map(function (item) {
var chapterTitle = item.chaptersTitle;
console.log('【' + chapterTitle + '】');
item.section.map(function (section) {
console.log(' 【' + section + '】')
})
})
}