1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      Node.js(十三)——Promise重構(gòu)爬蟲代碼

      在重構(gòu)代碼之前,先要了解下什么是https?

      按需求定制網(wǎng)站可以根據(jù)自己的需求進(jìn)行定制,網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站構(gòu)思過程中功能建設(shè)理應(yīng)排到主要部位公司網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站的運(yùn)用實(shí)際效果公司網(wǎng)站制作網(wǎng)站建立與制做的實(shí)際意義

      https協(xié)議:基于ssl/tls的http協(xié)議,所有的數(shù)據(jù)都是在

      ssl/tls協(xié)議的封裝之上傳輸?shù)模簿褪钦fhttps協(xié)議是在http協(xié)議基礎(chǔ)上

      添加了ssl/tls握手以及數(shù)據(jù)加密傳輸,因此這就是兩者之間最大的區(qū)別。

      https模塊專門處理加密訪問的,區(qū)別在于搭建https服務(wù)器的時(shí)候需要有

      重構(gòu)爬蟲代碼:

      var http = require('http')
      var cheerio = require('cheerio')
      //在新版本中Promise已經(jīng)內(nèi)置
      var promise = require('bluebird')
      var baseUrl = 'http://www.imooc.com/learn/'
      
      var videoIds = [348,259,197,134,751]
      
      function filterChapters(html){
      	var $ = cheerio.load(html)
      	var chapters = $('.mod-chapters')
      	//標(biāo)題
      	var title = $('#main .path span').text()
      	//學(xué)習(xí)的人數(shù)
      	//var number = parseInt($($('static-item')[0]).text().trim(),10)
      	//var number = $($('.static-item span')[1]).text();
      	//var number = parseInt($('.meta-value js-learn-num').text().trim(),10)
      	var number = $('.meta-value js-learn-num').html()
      	var courseData = {
      		title:title,
      		number:number,
      		videos:[]
      	}
      	//遍歷的里面拿到數(shù)據(jù)
      	chapters.each(function(item){
      		var chapter = $(this);
      		
      		//章節(jié)標(biāo)題
      		var chapterTitle = chapter.find('strong').text()
      		console.log(chapterTitle)
      		var videos = chapter.find('.video').children('li')
      		var chapterData = {
      			chapterTitle:chapterTitle,
      			videos:[]
      		}
      		
      		//遍歷videos
      		videos.each(function(item){
      			var video = $(this).find('.J-media-item')
      			var videoTitle = video.text()
      			var id = video.attr('href').split('video/')[1]
      			
      			chapterData.videos.push({
      				title:videoTitle,
      				id:id
      			})
      		})
      		
      		
      		courseData.videos.push(chapterData)
      	})
      	
      	return courseData
      }
      
      function printCourseInfo(coursesData){
      	
      	coursesData.forEach(function(courseData){
      		console.log(courseData.number + ' 人學(xué)過 '+courseData.title+'\n')
      	})
      	
      	//數(shù)組中的遍歷
      	coursesData.forEach(function(courseData){
      		console.log('### '+courseData.title+'\n')
      		courseData.videos.forEach(function(item){
      		
      		var chapterTitle = item.chapterTitle
      		item.videos.forEach(function(video){
      			console.log('【'+video.id+'】'+video.title);
      		})
      	})
      	})
      	
      }
      
      
      
      function getPageAsync(url){
      	return new Promise(function(resolve,reject){
      		console.log('正在抓取' + url  )
      		
      		http.get(url, function(res){
      		var html = ''
      		
      		res.on('data',function(data){
      			html += data
      		})
      		
      		res.on('end',function(){
      			//在請求完成的時(shí)候,通過resolve傳遞下去
      			resolve(html)
      		}).on('error',function(e){
      			//如果出錯(cuò)了
      			reject(e)
      			console.log('獲取頁面出錯(cuò)')
      		})
      	})
      		
      	})
      }
      
      var fetchCourseArray = []
      
      videoIds.forEach(function(id){
      	//遍歷的結(jié)果傳遞過去
      	 fetchCourseArray.push(getPageAsync(baseUrl + id))
      })
      
      
      //需要做并發(fā)控制,全部去爬
      Promise
      	.all(fetchCourseArray)
      	.then(function(pages){
      	//多頁面處理
      	var coursesData = []
      	//對page進(jìn)行加工
      	pages.forEach(function(html){
      		//對html進(jìn)行解析
      		var courses = filterChapters(html)
      		coursesData.push(courses)
      	})
      	
      	//遍歷
      	coursesData.sort(function(a,b){
      		return a.number < b.number
      	})
      	
      	printCourseInfo(coursesData)
      })

      運(yùn)行結(jié)果如下:

      Node.js(十三)——Promise重構(gòu)爬蟲代碼


      文章題目:Node.js(十三)——Promise重構(gòu)爬蟲代碼
      網(wǎng)站URL:
      http://www.ef60e0e.cn/article/pggehp.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        沾益县| 伽师县| 崇明县| 商城县| 类乌齐县| 延长县| 类乌齐县| 治县。| 旬阳县| 柳河县| 治多县| 合山市| 张家口市| 隆子县| 灌南县| 梓潼县| 遂溪县| 冀州市| 肃宁县| 六盘水市| 荆门市| 肇庆市| 永善县| 台山市| 阳朔县| 绥江县| 武威市| 太湖县| 潍坊市| 宁晋县| 綦江县| 辽阳市| 桃园市| 南宫市| 临安市| 大新县| 鸡西市| 开阳县| 米易县| 卢氏县| 额尔古纳市|