接著這篇文章Node.js+jade抓取博客所有文章生成靜態(tài)html文件的實(shí)例繼續(xù),在這篇文章中實(shí)現(xiàn)了采集與靜態(tài)文件的生成,在實(shí)際的采集項(xiàng)目中, 應(yīng)該是先入庫(kù)再選擇性的生成靜態(tài)文件。
那么我選擇的數(shù)據(jù)庫(kù)是mongodb,為什么用這個(gè)數(shù)據(jù)庫(kù),因?yàn)檫@個(gè)數(shù)據(jù)庫(kù)是基于集合,數(shù)據(jù)的操作基本是json,與dom模塊cheerio具有非常大的親和力,cheerio處理過(guò)濾出來(lái)的數(shù)據(jù),可以直接插入mongodb,不需要經(jīng)過(guò)任何的處理,非常的便捷,當(dāng)然跟node.js的親和力那就不用說(shuō)了,更重要的是,性能很棒。這篇文章我就不具體寫mongodb的基本用法,到時(shí)候會(huì)另起文章從0開(kāi)始寫mongodb基本常用用法.先看下入庫(kù)的效果與生成靜態(tài)文件的效果:


我在這個(gè)階段,把爬蟲(chóng)分離成2個(gè)模塊,采集入庫(kù)( crawler.js ), 生成靜態(tài)文件(makeHtml.js).
crawler.js:
var http = require('http');var cheerio = require('cheerio');var mongoose = require('mongoose');mongoose.Promise = global.Promise;var DB_URL = 'mongodb://localhost:27017/crawler';var aList = []; //博客文章列表信息var aUrl = []; //博客所有的文章urlvar db = mongoose.createConnection(DB_URL);db.on('connected', function (err) { if (err) { console.log(err); } else { console.log('db connected success'); }});var Schema = mongoose.Schema;var arcSchema = new Schema({ id: Number, //文章id title: String, //文章標(biāo)題 url: String, //文章鏈接 body: String, //文章內(nèi)容 entry: String, //摘要 listTime: Date //發(fā)布時(shí)間});var Article = db.model('Article', arcSchema);function saveArticle(arcInfo) { var arcModel = new Article(arcInfo); arcModel.save(function (err, result) { if (err) { console.log(err); } else { console.log(`${arcInfo['title']} 插入成功`); } });}function filterArticle(html) { var $ = cheerio.load(html); var arcDetail = {}; var title = $("#cb_post_title_url").text(); var href = $("#cb_post_title_url").attr("href"); var re = ///(/d+)/.html/; var id = href.match(re)[1]; var body = $("#cnblogs_post_body").html(); return { id: id, title: title, url: href, body: body };}function crawlerArc(url) { var html = ''; var str = ''; var arcDetail = {}; http.get(url, function (res) { res.on('data', function (chunk) { html += chunk; }); res.on('end', function () { arcDetail = filterArticle(html); saveArticle(arcDetail); if ( aUrl.length ) { setTimeout(function () { if (aUrl.length) { crawlerArc(aUrl.shift()); } }, 100); }else { console.log( '采集任務(wù)完成' ); return; } }); });}function filterHtml(html) { var $ = cheerio.load(html); var arcList = []; var aPost = $("#content").find(".post-list-item"); aPost.each(function () { var ele = $(this); var title = ele.find("h2 a").text(); var url = ele.find("h2 a").attr("href"); ele.find(".c_b_p_desc a").remove(); var entry = ele.find(".c_b_p_desc").text(); ele.find("small a").remove(); var listTime = ele.find("small").text(); var re = //d{4}-/d{2}-/d{2}/s*/d{2}[:]/d{2}/; listTime = listTime.match(re)[0]; arcList.push({ title: title, url: url, entry: entry, listTime: listTime }); }); return arcList;}function nextPage(html) { var $ = cheerio.load(html); var nextUrl = $("#pager a:last-child").attr('href'); if (!nextUrl) return getArcUrl(aList); var curPage = $("#pager .current").text(); if (!curPage) curPage = 1; var nextPage = nextUrl.substring(nextUrl.indexOf('=') + 1); if (curPage < nextPage) crawler(nextUrl);}function crawler(url) { http.get(url, function (res) { var html = ''; res.on('data', function (chunk) { html += chunk; }); res.on('end', function () { aList.push(filterHtml(html)); nextPage(html); }); });}function getArcUrl(arcList) { for (var key in arcList) { for (var k in arcList[key]) { aUrl.push(arcList[key][k]['url']); } } crawlerArc(aUrl.shift());}var url = 'http://www.cnblogs.com/ghostwu/';crawler(url);其他的核心模塊沒(méi)有怎么改動(dòng),主要增加了數(shù)據(jù)庫(kù)連接,數(shù)據(jù)庫(kù)創(chuàng)建,集合創(chuàng)建( 集合相當(dāng)于關(guān)系型數(shù)據(jù)庫(kù)中的表 ),Schema( 相當(dāng)于關(guān)系型數(shù)據(jù)庫(kù)的表結(jié)構(gòu) ).
mongoose操作數(shù)據(jù)庫(kù)( save:插入數(shù)據(jù) ).分離了文件生成模塊.
makeHtml.js文件
var fs = require('fs');var jade = require('jade');var mongoose = require('mongoose');mongoose.Promise = global.Promise;var DB_URL = 'mongodb://localhost:27017/crawler';var allArc = [];var count = 0;var db = mongoose.createConnection(DB_URL);db.on('connected', function (err) { if (err) { console.log(err); } else { console.log('db connected success'); }});var Schema = mongoose.Schema;var arcSchema = new Schema({ id: Number, //文章id title: String, //文章標(biāo)題 url: String, //文章鏈接 body: String, //文章內(nèi)容 entry: String, //摘要 listTime: Date //發(fā)布時(shí)間});var Article = db.model('Article', arcSchema);function makeHtml(arcDetail) { str = jade.renderFile('./views/layout.jade', arcDetail); ++count; fs.writeFile('./html/' + count + '.html', str, function (err) { if (err) { console.log(err); } console.log( `${arcDetail['id']}.html創(chuàng)建成功` + count ); if ( allArc.length ){ setTimeout( function(){ makeHtml( allArc.shift() ); }, 100 ); } });}function getAllArc(){ Article.find( {}, function( err, arcs ){ allArc = arcs; makeHtml( allArc.shift() ); } ).sort( { 'id' : 1 } );}getAllArc();以上這篇Node.js+jade+mongodb+mongoose實(shí)現(xiàn)爬蟲(chóng)分離入庫(kù)與生成靜態(tài)文件的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注