Node爬蟲

  • 使用cheerio爬蟲模塊
    抓取頁面后獲取元素信息跟jQuery基本一樣
const cheerio = require('cheerio');
const co = require('co');

/**
 * @method 提取起點(diǎn)此資源信息
 * @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');// 發(fā)送請求的工具類
            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)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 網(wǎng)絡(luò)爬蟲開發(fā) 第1章 課程介紹 什么是爬蟲 爬蟲的意義 課程內(nèi)容 前置知識 什么是爬蟲 可以把互聯(lián)網(wǎng)比做成一張“大...
    強(qiáng)某某閱讀 777評論 0 1
  • 前言 爬蟲一直是軟件工程師里看起來比較神秘高深的一門學(xué)問,它讓人們想起黑客,以及SEO等等。目前市面上也有專門的爬...
    南宮__閱讀 854評論 0 11
  • 我一直覺得,爬蟲是許多web開發(fā)人員難以回避的點(diǎn)。我們也應(yīng)該或多或少的去接觸這方面,因?yàn)榭梢詮呐老x中學(xué)習(xí)到web開...
    梔子花_ef39閱讀 825評論 0 2
  • 聲明:所有文章都是轉(zhuǎn)載整理的,只是為了自己學(xué)習(xí),方便自己觀看,如有侵權(quán),請立即聯(lián)系我,謝謝~ Node.js的學(xué)習(xí)...
    是河兔兔啊閱讀 1,077評論 0 0
  • 上篇文章介紹了爬蟲的基本概念和基本原理,這篇開始我們一起來看看具體如何從網(wǎng)頁中爬取自己想要的數(shù)據(jù)。 在下面爬蟲的栗...
    特慈閱讀 319評論 0 0