標籤:node.js(十三)——promise重構爬蟲代碼
在重構代碼之前,先要瞭解下什麼是https?
https協議:基於ssl/tls的http協議,所有的資料都是在
ssl/tls協議的封裝之上傳輸的,也就是說https協議是在http協議基礎上
添加了ssl/tls握手以及資料加密傳輸,因此這就是兩者之間最大的區別。
https模組專門處理加密訪問的,區別在於搭建https伺服器的時候需要有ssl認證。
類比搭建https伺服器
var https = require(‘https‘)var fs = require(‘fs‘)//檔案系統模組var options = {//同步的讀出ssl認證__虛擬key:fs.readFileSync(‘ssh_key.pem‘)cert:fs.readFileSync(‘ssh_cert.pem‘)}//通過以上讀取認證之後就可以運行https伺服器https.createServer(options, function(req,res){res.wirteHead(200)res.end(‘Hello Https‘)}).listen(8090)
重構爬蟲代碼:
var http = require(‘http‘)var cheerio = require(‘cheerio‘)//在新版本中Promise已經內建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‘)//標題var title = $(‘#main .path span‘).text()//學習的人數//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:[]}//遍曆的裡面拿到資料chapters.each(function(item){var chapter = $(this);//章區段標頭var chapterTitle = chapter.find(‘strong‘).text()console.log(chapterTitle)var videos = chapter.find(‘.video‘).children(‘li‘)var chapterData = {chapterTitle:chapterTitle,videos:[]}//遍曆videosvideos.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 + ‘ 人學過 ‘+courseData.title+‘\n‘)})//數組中的遍曆coursesData.forEach(function(courseData){console.log(‘### ‘+courseData.title+‘\n‘)courseData.videos.forEach(function(item){var chapterTitle = item.chapterTitleitem.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(){//在請求完成的時候,通過resolve傳遞下去resolve(html)}).on(‘error‘,function(e){//如果出錯了reject(e)console.log(‘擷取頁面出錯‘)})})})}var fetchCourseArray = []videoIds.forEach(function(id){//遍曆的結果傳遞過去 fetchCourseArray.push(getPageAsync(baseUrl + id))})//需要做並發控制,全部去爬Promise.all(fetchCourseArray).then(function(pages){//多頁面處理var coursesData = []//對page進行加工pages.forEach(function(html){//對html進行解析var courses = filterChapters(html)coursesData.push(courses)})//遍曆coursesData.sort(function(a,b){return a.number < b.number})printCourseInfo(coursesData)})
運行結果如下:
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/8F/50/wKiom1jahlPyvW7uAABnQMXU45E114.jpg" title="36020170315204858576.jpg" alt="wKiom1jahlPyvW7uAABnQMXU45E114.jpg" />
本文出自 “IT菜鳥” 部落格,請務必保留此出處http://mazongfei.blog.51cto.com/3174958/1911261
Node.js(十三)——Promise重構爬蟲代碼