最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于node下的http小爬蟲的示例代碼

 更新時間:2018年01月11日 12:04:05   作者:風慕李  
本篇文章主要介紹了基于node下的http小爬蟲的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

每時每刻不管你睡了還是沒睡,互聯(lián)網(wǎng)都會有海量的數(shù)據(jù)來來往往,有客服端到服務(wù)端,有服務(wù)端到服務(wù)端。http的get和request完成的角色即為數(shù)據(jù)的獲取及提交,接下來我們動手寫一個簡單的小爬蟲來爬爬菜鳥教程中關(guān)于node的章節(jié)的課程界面。

爬取Node.js 教程首頁的所有數(shù)據(jù)

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

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='';

  // 這里將會觸發(fā)data事件,不斷觸發(fā)不斷跟新html直至完畢
  res.on('data',function(data){
    html +=data
  })

  // 當數(shù)據(jù)獲取完成將會觸發(fā)end事件,這里將會打印初node官網(wǎng)的html
  res.on('end',function(){
    console.log(html)
  })
}).on('error',function(){
  console.log('獲取node官網(wǎng)相關(guān)數(shù)據(jù)出錯')
})

終端執(zhí)行結(jié)果中發(fā)現(xiàn)這個頁面的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"  />
<meta name="keywords" content="Node.js 教程,node,Node.js,nodejs">
<meta name="description" content="Node.js 教程  簡單的說 Node.js 就是運行在服務(wù)端的 JavaScript。 Node.js 是一個基于Chrome JavaScript 運行時建立的一個平臺
。 Node.js是一個事件驅(qū)動I/O服務(wù)端JavaScript環(huán)境,基于Google的V8引擎,V8引擎執(zhí)行Javascript的速度非常快,性能非常好。  誰適合閱讀本教程? 如果你是一個前端程序員,你不懂得像PHP、Python或Ruby等動態(tài)編程語言,..">
<link rel="shortcut icon"  rel="external nofollow" rel="external nofollow" mce_ 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"  rel="external nofollow" media="all" />
<!--[if gte IE 9]><!-->
。。。。。。。。。。
這里只展示部分不然你半天看不到頭

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

不過在此之前我們需要下載cheerio模塊(cheerio是nodejs的抓取頁面模塊,為服務(wù)器特別定制的,快速、靈活、實施的jQuery核心實現(xiàn)。適合各種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官網(wǎng)地址變量
// filer node chapter
function filerNodeChapter(html){
  // 將爬取得HTML裝載起來
  var $=cheerio.load(html);
  // 拿到左側(cè)邊欄的每個目錄
  var nodeChapter=$('#leftcolumn a');
  //這里我希望我能獲取的到的最終數(shù)據(jù)格式這個樣子的,如此我們能知道每個目錄的地址及標題
  /**
   * [{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;

}

//獲取每個數(shù)據(jù)
function getChapterData(nodeChapter){
  nodeChapter.forEach(function(item){
    console.log(' 【 '+item.id+' 】'+item.title+'\n')
  });
}

http.get(url,function(res){
  var html='';

  // 這里將會觸發(fā)data事件,不斷觸發(fā)不斷跟新html直至完畢
  res.on('data',function(data){
    html +=data
  })

  // 當數(shù)據(jù)獲取完成將會觸發(fā)end事件,這里將會打印初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ù)出錯')
})

終端執(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)建第一個應(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)
。。。。。。。。。。。
這里就不全部給出,你可以自己嘗試著運行操作查看所有結(jié)果

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

您可能感興趣的文章:

相關(guān)文章

最新評論

贵州省| 图片| 佳木斯市| 滦南县| 奇台县| 五莲县| 南乐县| 廉江市| 营山县| 建水县| 思南县| 老河口市| 汉阴县| 府谷县| 简阳市| 河源市| 周口市| 辽阳市| 华宁县| 康马县| 广丰县| 综艺| 高邮市| 沽源县| 南部县| 潜山县| 昂仁县| 阿城市| 景洪市| 永安市| 蕉岭县| 鸡西市| 珲春市| 云和县| 陆川县| 萝北县| 邵阳市| 吴旗县| 沅陵县| 灵武市| 广灵县|