国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

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

2024-05-06 16:41:56
字體:
來源:轉載
供稿:網友

每時每刻不管你睡了還是沒睡,互聯網都會有海量的數據來來往往,有客服端到服務端,有服務端到服務端。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    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 模塊系統。。。。。。。。。。。這里就不全部給出,你可以自己嘗試著運行操作查看所有結果

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


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黑龙江省| 黄山市| 虹口区| 巴林右旗| 甘孜| 千阳县| 嫩江县| 崇阳县| 建平县| 平江县| 重庆市| 浮山县| 安康市| 白水县| 西宁市| 江川县| 九寨沟县| 峨边| 维西| 吐鲁番市| 师宗县| 富蕴县| 邯郸市| 无为县| 浏阳市| 太湖县| 如东县| 庆云县| 革吉县| 城口县| 贵州省| 兰溪市| 枝江市| 星子县| 枣强县| 龙川县| 高阳县| 桐庐县| 临沭县| 泾川县| 济阳县|