首頁 > web前端 > js教程 > 主體

在node如何實現http小爬蟲

亚连
發布: 2018-06-12 15:04:12
原創
1388 人瀏覽過

本篇文章主要介紹了基於node下的http小爬蟲的範例程式碼,現在分享給大家,也給大家做個參考。

每時每刻不管你睡了還是沒睡,網路都會有海量的資料來來往往,有客服端到服務端,有服務端到服務端。 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=&#39;dns-prefetch&#39; href=&#39;//s.w.org&#39; />
<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(&#39;http&#39;);//获取http模块
var cheerio=require(&#39;cheerio&#39;);//引入cheerio模块
var url=&#39;http://www.runoob.com/nodejs/nodejs-tutorial.html&#39;;//定义node官网地址变量
// filer node chapter
function filerNodeChapter(html){
  // 将爬取得HTML装载起来
  var $=cheerio.load(html);
  // 拿到左侧边栏的每个目录
  var nodeChapter=$(&#39;#leftcolumn a&#39;);
  //这里我希望我能获取的到的最终数据格式这个样子的,如此我们能知道每个目录的地址及标题
  /**
   * [{id:,title:}]
   */
  var chapterData=[];
  nodeChapter.each(function(item){
    // 获取每项的地址及标题
    var id=$(this).attr(&#39;href&#39;);
    var title=$(this).text();
    chapterData.push({
      id:id,
      title:title
    })
  })

  return chapterData;

}

//获取每个数据
function getChapterData(nodeChapter){
  nodeChapter.forEach(function(item){
    console.log(&#39; 【 &#39;+item.id+&#39; 】&#39;+item.title+&#39;\n&#39;)
  });
}

http.get(url,function(res){
  var html=&#39;&#39;;

  // 这里将会触发data事件,不断触发不断跟新html直至完毕
  res.on(&#39;data&#39;,function(data){
    html +=data
  })

  // 当数据获取完成将会触发end事件,这里将会打印初node官网的html
  res.on(&#39;end&#39;,function(){
    //console.log(html)
    // 过滤出node.js的课程目录
    var nodeChapter= filerNodeChapter(html);

    //循环打印所获取的数据
    getChapterData(nodeChapter)
  })
}).on(&#39;error&#39;,function(){
  console.log(&#39;获取node官网相关数据出错&#39;)
})
登入後複製

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

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 模块系统
。。。。。。。。。。。
这里就不全部给出,你可以自己尝试着运行操作查看所有结果
登入後複製

上面是我整理給大家的,希望今後會對大家有幫助。

相關文章:

在React、Vue專案中如何使用SVG

透過JavaScript實作比較同一天的時間大小

使用vue2.0.js實作多級連動選擇器

#使用mint-ui實作省市區三級連動效果

使用vue實作二級路由設定方法

在Vue-Router2.X中實作多種路由實作

#

以上是在node如何實現http小爬蟲的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!