您好,登錄后才能下訂單哦!
這篇文章給大家介紹node中怎么實(shí)現(xiàn)一個(gè)http小爬蟲,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
爬取Node.js 教程首頁的所有數(shù)據(jù)
建立node-http.js,其中代碼如下,代碼中有詳細(xì)的的注釋,自行理解了哈
var http=require('http');//獲取http模塊 var url='http://www.runoob.com/nodejs/nodejs-tutorial.html';//定義node官網(wǎng)地址變量 http.get(url,function(res){ var html=''; // 這里將會(huì)觸發(fā)data事件,不斷觸發(fā)不斷跟新html直至完畢 res.on('data',function(data){ html +=data }) // 當(dāng)數(shù)據(jù)獲取完成將會(huì)觸發(fā)end事件,這里將會(huì)打印初node官網(wǎng)的html res.on('end',function(){ console.log(html) }) }).on('error',function(){ console.log('獲取node官網(wǎng)相關(guān)數(shù)據(jù)出錯(cuò)') })
終端執(zhí)行結(jié)果中發(fā)現(xiàn)這個(gè)頁面的html全部被爬下來了
G:\node\node-http> node node-http.js <!Doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta property="qc:admins" content="465267610762567726375" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Node.js 教程 | 菜鳥教程</title> <link rel='dns-prefetch' href='//s.w.org' /> <link rel="canonical" href="http://www.runoob.com/nodejs/nodejs-tutorial.html" /> <meta name="keywords" content="Node.js 教程,node,Node.js,nodejs"> <meta name="description" content="Node.js 教程 簡單的說 Node.js 就是運(yùn)行在服務(wù)端的 JavaScript。 Node.js 是一個(gè)基于Chrome JavaScript 運(yùn)行時(shí)建立的一個(gè)平臺(tái) 。 Node.js是一個(gè)事件驅(qū)動(dòng)I/O服務(wù)端JavaScript環(huán)境,基于Google的V8引擎,V8引擎執(zhí)行Javascript的速度非常快,性能非常好。 誰適合閱讀本教程? 如果你是一個(gè)前端程序員,你不懂得像PHP、Python或Ruby等動(dòng)態(tài)編程語言,.."> <link rel="shortcut icon" href="//static.runoob.com/images/favicon.ico" rel="external nofollow" rel="external nofollow" mce_href="//static.runoob.com/images/favicon.ico" rel="external nofollow" rel="external nofollow" type="image/x-icon"> <link rel="stylesheet" href="/wp-content/themes/runoob/style.css?v=1.141" rel="external nofollow" type="text/css" media="all" /> <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="external nofollow" media="all" /> <!--[if gte IE 9]><!--> 。。。。。。。。。。 這里只展示部分不然你半天看不到頭
當(dāng)然爬個(gè)HTML對(duì)于我們來說沒啥用,現(xiàn)在我們要做些過濾,比如這個(gè)node教程中我想知道課程目錄有哪些,這樣可以選擇感興趣的去看看學(xué)學(xué)。直接上代碼吧還是:
不過在此之前我們需要下載cheerio模塊(cheerio是nodejs的抓取頁面模塊,為服務(wù)器特別定制的,快速、靈活、實(shí)施的jQuery核心實(shí)現(xiàn)。適合各種Web爬蟲程序。)具體詳細(xì)介紹你們可以自行去搜索了解,cheerio的用跟jquery的用法非常類似,所以不用擔(dān)心上手繁瑣。
PS G:\node\node-http> npm install cheerio
建立node-http-more.js,其中代碼如下:
var http=require('http');//獲取http模塊 var cheerio=require('cheerio');//引入cheerio模塊 var url='http://www.runoob.com/nodejs/nodejs-tutorial.html';//定義node官網(wǎng)地址變量 // filer node chapter function filerNodeChapter(html){ // 將爬取得HTML裝載起來 var $=cheerio.load(html); // 拿到左側(cè)邊欄的每個(gè)目錄 var nodeChapter=$('#leftcolumn a'); //這里我希望我能獲取的到的最終數(shù)據(jù)格式這個(gè)樣子的,如此我們能知道每個(gè)目錄的地址及標(biāo)題 /** * [{id:,title:}] */ var chapterData=[]; nodeChapter.each(function(item){ // 獲取每項(xiàng)的地址及標(biāo)題 var id=$(this).attr('href'); var title=$(this).text(); chapterData.push({ id:id, title:title }) }) return chapterData; } //獲取每個(gè)數(shù)據(jù) function getChapterData(nodeChapter){ nodeChapter.forEach(function(item){ console.log(' 【 '+item.id+' 】'+item.title+'\n') }); } http.get(url,function(res){ var html=''; // 這里將會(huì)觸發(fā)data事件,不斷觸發(fā)不斷跟新html直至完畢 res.on('data',function(data){ html +=data }) // 當(dāng)數(shù)據(jù)獲取完成將會(huì)觸發(fā)end事件,這里將會(huì)打印初node官網(wǎng)的html res.on('end',function(){ //console.log(html) // 過濾出node.js的課程目錄 var nodeChapter= filerNodeChapter(html); //循環(huán)打印所獲取的數(shù)據(jù) getChapterData(nodeChapter) }) }).on('error',function(){ console.log('獲取node官網(wǎng)相關(guān)數(shù)據(jù)出錯(cuò)') })
終端執(zhí)行結(jié)果及打印出課程目錄
G:\node\node-http> node node-http-more.js 【 /nodejs/nodejs-tutorial.html 】 Node.js 教程 【 /nodejs/nodejs-install-setup.html 】 Node.js 安裝配置 【 /nodejs/nodejs-http-server.html 】 Node.js 創(chuàng)建第一個(gè)應(yīng)用 【 nodejs-npm.html 】 NPM 使用介紹 【 nodejs-repl.html 】 Node.js REPL 【 nodejs-callback.html 】 Node.js 回調(diào)函數(shù) 【 nodejs-event-loop.html 】 Node.js 事件循環(huán) 【 nodejs-event.html 】 Node.js EventEmitter 【 nodejs-buffer.html 】 Node.js Buffer 【 nodejs-stream.html 】 Node.js Stream 【 /nodejs/nodejs-module-system.html 】 Node.js 模塊系統(tǒng) 。。。。。。。。。。。 這里就不全部給出,你可以自己嘗試著運(yùn)行操作查看所有結(jié)果
關(guān)于node中怎么實(shí)現(xiàn)一個(gè)http小爬蟲就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(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)容。