- 使用cheerio爬蟲(chóng)模塊
抓取頁(yè)面后獲取元素信息跟jQuery基本一樣
const cheerio = require('cheerio');
const co = require('co');
/**
* @method 提取起點(diǎn)此資源信息
* @param id 小說(shuō)id(從列表頁(yè)爬取到的)
* @returns {Function}
*/
seachInfo(id) {
return function (cb) {
co(function* () {
let result = yield Util.req.sendReq('http://book.qidian.com/info/' + id, 'GET', '', 'crawler');// 發(fā)送請(qǐng)求的工具類
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);
})
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。