- 使用cheerio爬虫模块
抓取页面后获取元素信息跟jQuery基本一样
const cheerio = require('cheerio');
const co = require('co');
/**
* @method 提取起点此资源信息
* @param id 小说id(从列表页爬取到的)
* @returns {Function}
*/
seachInfo(id) {
return function (cb) {
co(function* () {
let result = yield Util.req.sendReq('http://book.qidian.com/info/' + id, 'GET', '', 'crawler');// 发送请求的工具类
let $ = cheerio.load(result, {decodeEntities: false}); //采用cheerio模块解析html
let novels = {};
novels.img = $(".book-information .book-img img").attr('src');
novels.name = $(".book-information .book-info h1 em").html();
novels.author = $(".book-information .book-info .writer").html();
let wordsNum = $(".book-information .book-info p em").eq(0).html();
novels.wordsNum = parseInt(wordsNum) * 10000;
novels.summary = $('.book-intro p').text();
cb(null, novels);
}).catch(function (err) {
cb(new Error(err.message), null);
})
}
}