基於node下的http小爬蟲的範例程式碼,node爬蟲範例程式碼

來源:互聯網
上載者:User

基於node下的http小爬蟲的範例程式碼,node爬蟲範例程式碼

每時每刻不管你睡了還是沒睡,互連網都會有海量的資料來來往往,有客服端到服務端,有服務端到服務端。http的get和request完成的角色即為資料的擷取及提交,接下來我們動手寫一個簡單的小爬蟲來爬爬菜鳥教程中關於node的章節的課程介面。

爬取Node.js 教程首頁的所有資料

建立node-http.js,其中代碼如下,代碼中有詳細的的注釋,自行理解了哈

var http=require('http');//擷取http模組var url='http://www.runoob.com/nodejs/nodejs-tutorial.html';//定義node官網地址變數http.get(url,function(res){  var html='';  // 這裡將會觸發data事件,不斷觸發不斷跟新html直至完畢  res.on('data',function(data){    html +=data  })  // 當資料擷取完成將會觸發end事件,這裡將會列印初node官網的html  res.on('end',function(){    console.log(html)  })}).on('error',function(){  console.log('擷取node官網相關資料出錯')})

終端執行結果中發現這個頁面的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 就是運行在服務端的 JavaScript。 Node.js 是一個基於Chrome JavaScript 運行時建立的一個平台。 Node.js是一個事件驅動I/O服務端JavaScript環境,基於Google的V8引擎,V8引擎執行Javascript的速度非常快,效能非常好。  誰適合閱讀本教程? 如果你是一個前端程式員,你不懂得像PHP、Python或Ruby等動態程式設計語言,.."><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]><!-->。。。。。。。。。。這裡只展示部分不然你半天看不到頭

當然爬個HTML對於我們來說沒啥用,現在我們要做些過濾,比如這個node教程中我想知道課程目錄有哪些,這樣可以選擇感興趣的去看看學學。直接上代碼吧還是:

不過在此之前我們需要下載cheerio模組(cheerio是nodejs的抓取頁面模組,為伺服器特別定製的,快速、靈活、實施的jQuery核心實現。適合各種Web爬蟲程式。)具體詳細介紹你們可以自行去搜尋瞭解,cheerio的用跟jquery的用法非常類似,所以不用擔心上手繁瑣。

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官網地址變數// filer node chapterfunction filerNodeChapter(html){  // 將爬取得HTML裝載起來  var $=cheerio.load(html);  // 拿到左側邊欄的每個目錄  var nodeChapter=$('#leftcolumn a');  //這裡我希望我能擷取的到的最終資料格式這個樣子的,如此我們能知道每個目錄的地址及標題  /**   * [{id:,title:}]   */  var chapterData=[];  nodeChapter.each(function(item){    // 擷取每項的地址及標題    var id=$(this).attr('href');    var title=$(this).text();    chapterData.push({      id:id,      title:title    })  })  return chapterData;}//擷取每個資料function getChapterData(nodeChapter){  nodeChapter.forEach(function(item){    console.log(' 【 '+item.id+' 】'+item.title+'\n')  });}http.get(url,function(res){  var html='';  // 這裡將會觸發data事件,不斷觸發不斷跟新html直至完畢  res.on('data',function(data){    html +=data  })  // 當資料擷取完成將會觸發end事件,這裡將會列印初node官網的html  res.on('end',function(){    //console.log(html)    // 過濾出node.js的課程目錄    var nodeChapter= filerNodeChapter(html);    //迴圈列印所擷取的資料    getChapterData(nodeChapter)  })}).on('error',function(){  console.log('擷取node官網相關資料出錯')})

終端執行結果及列印出課程目錄

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 建立第一個應用 【 nodejs-npm.html 】 NPM 使用介紹 【 nodejs-repl.html 】 Node.js REPL 【 nodejs-callback.html 】 Node.js 回呼函數 【 nodejs-event-loop.html 】 Node.js 事件迴圈 【 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 模組系統。。。。。。。。。。。這裡就不全部給出,你可以自己嘗試著運行操作查看所有結果

到此一個簡單的爬蟲就寫完了,趕緊自己動手試試吧,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.