- 使用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);
})
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。