您好,登錄后才能下訂單哦!
接著這篇文章Node.js+jade抓取博客所有文章生成靜態(tài)html文件的實(shí)例繼續(xù),在這篇文章中實(shí)現(xiàn)了采集與靜態(tài)文件的生成,在實(shí)際的采集項(xiàng)目中, 應(yīng)該是先入庫再選擇性的生成靜態(tài)文件。
那么我選擇的數(shù)據(jù)庫是mongodb,為什么用這個數(shù)據(jù)庫,因?yàn)檫@個數(shù)據(jù)庫是基于集合,數(shù)據(jù)的操作基本是json,與dom模塊cheerio具有非常大的親和力,cheerio處理過濾出來的數(shù)據(jù),可以直接插入mongodb,不需要經(jīng)過任何的處理,非常的便捷,當(dāng)然跟node.js的親和力那就不用說了,更重要的是,性能很棒。這篇文章我就不具體寫mongodb的基本用法,到時候會另起文章從0開始寫mongodb基本常用用法.先看下入庫的效果與生成靜態(tài)文件的效果:
我在這個階段,把爬蟲分離成2個模塊,采集入庫( 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 = []; //博客所有的文章url 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ā)布時間 }); 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("h3 a").text(); var url = ele.find("h3 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);
其他的核心模塊沒有怎么改動,主要增加了數(shù)據(jù)庫連接,數(shù)據(jù)庫創(chuàng)建,集合創(chuàng)建( 集合相當(dāng)于關(guān)系型數(shù)據(jù)庫中的表 ),Schema( 相當(dāng)于關(guān)系型數(shù)據(jù)庫的表結(jié)構(gòu) ).
mongoose操作數(shù)據(jù)庫( 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ā)布時間 }); 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)爬蟲分離入庫與生成靜態(tài)文件的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。