Node.js製做爬取簡書內容的爬蟲

用了Nodejs製做了簡單的爬蟲,爬取了簡書的文章內容,代碼中contentIds有幾個,就爬取了幾個網頁的內容。html

能夠直接看結尾截圖的結果。node

 

下面兩張截圖,是說明怎麼用cheerio獲取本身想要的內容。jquery

下面截圖,是代碼中:第46行,利用cheerio模塊,獲取版塊標題內容npm

 

下面截圖:代碼第50行,利用cheerio獲取做者數組

 

代碼中:var contentIds = ["074e475b2f45","Jgq3Wc","8c92f845cd4d","V2CqjW"],從下圖來。引入要爬取多個網頁,因此就得拼接url.而後利用Promise的回調特性,最後將內容都打印出來。app

  1 // nodejs官方文檔:http://nodejs.cn/
  2 // 引入http模塊
  3 var http = require('http')
  4 
  5 // bluebird模塊若是引用,必須下載 npm install bluebird
  6 // nodejs自己有Promise,不引入也能夠。
  7 // 引入bluebird也是爲了引入Promise,callback效率可能必nodejs自己的快。可能!!
  8 // bluebird學習連接:http://ricostacruz.com/cheatsheets/bluebird.html
  9 var Promise = require('bluebird')
 10 
 11 // 引入cheerio,相似jquery庫,必須下載,npm install cheerio
 12 // cheerio文檔:https://www.npmjs.com/package/cheerio
 13 // 中文文檔:https://cnodejs.org/topic/5203a71844e76d216a727d2e
 14 // 用法跟jquery相似
 15 var cheerio = require('cheerio')
 16 
 17 // 路徑模塊
 18 var path = require('path')
 19 
 20 // 文件系統模塊
 21 var fs = require('fs')
 22 
 23 // 簡書的通用的url
 24 var url = 'http://www.jianshu.com/c/'
 25 
 26 // 簡書每一個頁面url不一樣的部分
 27 // 版塊內容:
 28     // 074e475b2f45:成長勵志
 29     // Jgq3Wc:上班這點事兒
 30     // 8c92f845cd4d:漫畫·手繪
 31     // V2CqjW:@IT·互聯網
 32 var contentIds = ["074e475b2f45","Jgq3Wc","8c92f845cd4d","V2CqjW"]
 33 
 34 // 過濾本身想要的數據,放到對象裏面
 35 function filterData(html) {
 36     var $ = cheerio.load(html)
 37     var lis = $('.have-img')
 38     // 每一個版塊的內容
 39     var board = {
 40         // 版塊標題
 41         title:'',
 42         // 版塊文章,裏面是做者信息和文章標題,和連接
 43         articles:[]
 44     }
 45     // 獲取版塊標題
 46     board.title = $('div.title').children('a').text()
 47     lis.each(function (item,value) {
 48         var lis = $(this)
 49         // 獲取做者
 50         var author = lis.find('.name').children('a').text()
 51         // 獲取文章標題
 52         var title = lis.find('.content').children('a').text()
 53         // 獲取文章連接
 54         var link = lis.find('.content').children('a').attr('href')
 55         board.articles.push({author:author, title:title, link:"http://www.jianshu.com"+link})
 56 
 57     })
 58     return board
 59 }
 60 // 打印內容到控制面板
 61 function printData(board) {
 62     console.log(board.title + "\n")
 63     board.articles.forEach(function (value) {
 64         console.log("做者:"+"\t"+value.author +"\t"+ "標題:"+"\t"+value.title +"\t"+"連接:"+"\t"+value.link )
 65     })
 66     console.log("\n")
 67 }
 68 // 將數據寫入文件
 69 function writeToFile(board) {
 70     // 拼接數據內容
 71     var html = '\n ' + '版塊:' + board.title + '\n'
 72     board.articles.forEach(function (value) {
 73         html += "做者:"+"\t"+value.author +"\t"+ "標題:"+"\t"+value.title +"\t"+"連接:"+"\t"+value.link +"\n"
 74     })
 75     // 將數據寫入到文件,文件路徑爲當前路徑,文件名是jianshu.txt
 76     fs.appendFileSync(path.join(__dirname,'jianshu.txt'),html)
 77 }
 78 // 獲取簡書內容
 79 function getJianShuContent(url) {
 80     // 經過Promise獲取簡書內容,而且返回
 81     return new Promise(function (resolve,reject) {
 82         http.get(url,(res)=>{
 83             var html = ''
 84             res.on('data',(data)=>{
 85                 // 獲取到的數據拼接到HTML中
 86                 html += data
 87             })
 88             res.on('end',function () {
 89                 // 結束後將數據放到resolve
 90                 resolve(html)
 91             })
 92         }).on('error',function (e) {
 93             reject(e)
 94             console.log('獲取數據錯誤!!!!!!!!')
 95         })
 96     })
 97 }
 98 var jianShuContent = []
 99 
100 contentIds.forEach(function (id) {
101     // 將獲取到的放到數組中,利用Promise的特性
102     jianShuContent.push(getJianShuContent(url + id))
103 })
104 // 迭代數組,而後遍歷,打印內容,寫入內容
105 Promise.all(jianShuContent).then(function (pages) {
106     pages.forEach(function (value) {
107         var board = filterData(value)
108         printData(board)
109         writeToFile(board)
110     })
111 })

 

控制面板輸出結果:學習

 

寫入文件結果:ui

相關文章
相關標籤/搜索