node.js實(shí)現(xiàn)博客小爬蟲的實(shí)例代碼
前言
爬蟲,是一種自動(dòng)獲取網(wǎng)頁內(nèi)容的程序。是搜索引擎的重要組成部分,因此搜索引擎優(yōu)化很大程度上就是針對(duì)爬蟲而做出的優(yōu)化。
這篇文章介紹的是利用node.js實(shí)現(xiàn)博客小爬蟲,核心的注釋我都標(biāo)注好了,可以自行理解,只需修改url和按照要趴的博客內(nèi)部dom構(gòu)造改一下filterchapters和filterchapters1就行了!
下面話不多說,直接來看實(shí)例代碼
var http=require('http');
var Promise=require('Bluebird');
var cheerio = require('cheerio');
var url='http://www.immaster.cn';//博客地址
function filterchapters1(html) {//解析文章鏈接
var $ =cheerio.load(html);
var post=$('.post');
var content=[];
post.each(function (item) {
var postid=$(this).find('.tit').find('a').attr('href');
content.push(postid);
})
return content;
}
function filterchapters(html) {//解析每個(gè)文章內(nèi)的內(nèi)容
var $ =cheerio.load(html);
var tit=$('.post .tit').find('a').text();
var postid=$('.tit').find('a').attr('href');
var commentnum=$('.comments-title').text();
commentnum=commentnum.trim();
// commentnum=commentnum.replace('\n','');
var content={tit:tit,url:postid,commentnum:commentnum};
return content;
}
function getid(url){//爬取首頁文章鏈接
return new Promise(function (resolve,reject) {
http.get(url,function (res) {
var html = '';
res.on('data',function(data) {
html+=data;
});
res.on('end',function () {
var content=filterchapters1(html)
resolve(content);
})
}).on('error',function () {
reject(e);
console.log('抓取出錯(cuò)!')
})
})
}
function getpageAsync(url) {//爬取單個(gè)頁面內(nèi)容
return new Promise(function (resolve,reject) {
console.log('正在爬取……'+url)
http.get(url,function (res) {
var html = '';
res.on('data',function(data) {
html+=data;
});
res.on('end',function () {
resolve(html);
})
}).on('error',function () {
reject(e);
console.log('抓取出錯(cuò)!')
})
})
}
getid(url)
.then(function(postid){
return new Promise(function (resolve,reject) {
var pageurls=[];
postid.forEach(function (id) {
pageurls.push(getpageAsync(id));
})
resolve(pageurls);
})
})
.then(function(pageurls){
return new Promise.all(pageurls);//讓promise對(duì)象同時(shí)開始運(yùn)行
})
.then(function (pages) {
var coursesData=[];
pages.forEach(function (html) {
var courses=filterchapters(html);
coursesData.push(courses);
})
coursesData.forEach(function(v){
console.log('標(biāo)題:'+v.tit+"\n地址:"+v.url+"\n評(píng)論:"+v.commentnum)
})
})
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用node.js實(shí)現(xiàn)爬蟲能有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Node.js操作Firebird數(shù)據(jù)庫教程
這篇文章主要為大家分享了Node.js操作Firebird數(shù)據(jù)庫教程,思路清晰便于大家理解,感興趣的小伙伴們可以參考一下2016-03-03
nodejs使用express創(chuàng)建一個(gè)簡單web應(yīng)用
這篇文章主要介紹了nodejs使用express創(chuàng)建一個(gè)簡單web應(yīng)用的相關(guān)資料,需要的朋友可以參考下2017-03-03
node使用promise替代回調(diào)函數(shù)
這篇文章主要介紹了node使用promise替代回調(diào)函數(shù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
簡單聊一聊Node.js參數(shù)max-old-space-size
簡單的說Node.js就是運(yùn)行在服務(wù)端的JavaScript,下面這篇文章主要給大家介紹了關(guān)于Node.js參數(shù)max-old-space-size的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
npm安裝windows-build-tools卡在Successfully?installed?Python2.7
這篇文章主要介紹了npm安裝windows-build-tools卡在Successfully?installed?Python2.7的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
websocket結(jié)合node.js實(shí)現(xiàn)雙向通信的示例代碼
本文主要介紹了websocket結(jié)合node.js實(shí)現(xiàn)雙向通信的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
nodejs基于express實(shí)現(xiàn)文件上傳的方法
這篇文章主要介紹了nodejs基于express實(shí)現(xiàn)文件上傳的方法,結(jié)合實(shí)例形式分析了nodejs基于express框架實(shí)現(xiàn)文件上傳功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
nodejs+koa2 實(shí)現(xiàn)模仿springMVC框架
這篇文章主要介紹了nodejs+koa2 實(shí)現(xiàn)模仿springMVC框架,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Node.js+jade抓取博客所有文章生成靜態(tài)html文件的實(shí)例
下面小編就為大家?guī)硪黄狽ode.js+jade抓取博客所有文章生成靜態(tài)html文件的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09

