Node命令行工具開發【看段子小工具】

Node命令行工具開發【看段子】

你有沒有上班想看笑話卻又怕領導發現的經歷?如今咱們就用幾十行代碼寫一個命令行看笑話段子的小程序,今後無需擔憂領導的視察。這篇文章和上一篇差很少都是命令行小工具開發,不過本篇更偏向於小爬蟲的開發

總覽:命令行看段子小程序

咱們先來看看咱們今天的小目標:javascript

  • 先爲它起個命吧:joke-cli
  • 爬取並提取 糗事百科的笑話
  • 輸出到命令行按下回車顯示一條段子

初識[新手村]

下面咱們將介紹今天用到的主要模塊html

cheerio

cheerio可理解爲服務器端的jQuery,基本用法與jQuery同樣。有了它,咱們在寫小爬蟲時就可拋開那可愛又可恨的正則表達式了。今後擁抱幸福生活。

用法以下:java

let cheerio = require('cheerio')

// 將html文本轉化成可用jQuery操做的dom,,而後你就能夠把它當成jQuery了
let $ = cheerio.load('<h2 class="title">Hello world</h2>')
 
$('h2.title').text('Hello there!')
$('h2').addClass('welcome')
 
$.html()
//=> <h2 class="title welcome">Hello there!</h2>

更多請參考cheerionode

superagent

superagent專一於處理服務端/客戶端的http請求,用法以下:git

request
   .get(url)
   .end(function(err, res){

   });

就是這麼簡答,你已經學會使用它了,別懵逼,這三行代碼,已經徹底夠咱們本次示例的使用了。額,固然了,更多的請移步官方文檔github

初出茅廬【先拿百度練練手】

咱們已經介紹了今天的兩大主角,那就先來練練手,就用百度吧web

const superAgent = require('superagent')
const cheerio = require('cheerio')
const URL = 'http://www.baidu.com/'

// 發起GET請求
superAgent
  .get(URL) 
  .end((err, res)=>{
      if(err) console.error(err)
    // 用cheerio處理返回的html
      const $ = cheerio.load(res.text)
    // 找到並打印出按鈕#su的值
    console.log($('#su').val())  // 百度一下
  })

ok,如今已經瞭解並能使用這兩個模塊,更多的探索在本身,咱們應當再用寫時間去閱讀其文檔,對其有深刻的瞭解。接下來就是用剛學的這些知識爲你漲姿式了。正則表達式

開始開發joke-cli

初始化項目

$ mkdir joke-cli
$ cd joke-cli
$ npm init
$ npm install cheerio superagent colors --save //colors 輸出美化

新建好bin/index.js文件,並加入package.json文件中npm

"bin": {
    "joke-cli": "./bin/index.js"
 }

如今執行npm link。咱們的小項目就初始化成功了,就能夠認真思考代碼了json

首先分析糗事百科url

咱們打開糗事百科會發現它的URL仍是很簡單,因爲咱們只是爬取段子因此url以下http://www.qiushibaike.com/text/page/2/4965899,2就是頁數。

糗事百科

開始編寫index.js

#!/usr/bin/env node

const superAgent = require('superagent')
const cheerio = require('cheerio')

let url = 'http://www.qiushibaike.com/text/page/'
let page = 1

superAgent
    .get(url+page) 
    .end((err, res)=>{
          if(err) console.error(err)
        console.log(res.text)
    })

f12查看糗事百科的HTML結構,能夠發先每條笑話都在一個div.article中,笑話的內容則在div.content

糗事百科HTML

在分析完html結夠後,如今能夠用咱們用cheerio很容易就能夠取出笑話

···
.end((err, res)=>{
  if(err) console.error(err)
  const $ = cheerio.load(res.text)
  const jokeList = $('.article .content span')
  jokeList.each(function(i, item){
    console.log($(this).text()) //將打印出每條笑話
  })    
})    
···

實現命令行交互

這裏咱們須要用到readlinem模塊

Readline(逐行讀取)

require('readline') 模塊提供了一個接口,用於從 可讀流(如 process.stdin)讀取數據,每次讀取一行。 基本用法以下:

使用 readline.Interface 類實現一個簡單的命令行界面:

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt: '請輸入> '
});

//rl.prompt() 方法會在 output 流中新的一行寫入 readline.Interface 實例配置後的 prompt,用於爲用戶提供一個可供輸入的新的位置
rl.prompt();

rl.on('line', (line) => {
  switch(line.trim()) {
    case 'hello':
      console.log('world!');
      break;
    default:
      console.log(`你輸入的是:'${line.trim()}'`);
      break;
  }
  rl.prompt();
}).on('close', () => {
  console.log('再見!');
  process.exit(0);
});

更多參考Node.js中文網

改寫index.js實現命令行交互功能

#!/usr/bin/env node

const superAgent = require('superagent')
const cheerio = require('cheerio')
const readline = require('readline')
const colors = require('colors')
// 建立readlinde.Interface 實現命令行交互
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    prompt: ' ?  您正在使用joke-cli,按下回車查看笑話 ? >>>'
})
let url = 'http://www.qiushibaike.com/text/page/'
let page = 1

// 使用數組來存放笑話
let jokeStories = []
// 載入笑話並存入數組中
function loadJokes(){
      // 數組中的笑話不足三條時就請求下一頁的數據
    if(jokeStories.length<3){
        superAgent
        .get(url+page) 
        .end((err, res)=>{
            if(err) console.error(err)
            const $ = cheerio.load(res.text)
            const jokeList = $('.article .content span')
            jokeList.each(function(i, item){
                jokeStories.push($(this).text()) //存入數組
            })
            page++            
        })
    }
}

rl.prompt()
loadJokes()
// line事件 每當 input 流接收到接收行結束符(\n、\r 或 \r\n)時觸發 'line' 事件。 一般發生在用戶按下 <Enter> 鍵或 <Return> 鍵。
// 按下回車鍵顯示一條笑話
rl.on('line', (line) => {
    if(jokeStories.length>0){
        console.log('======================')
        console.log(jokeStories.shift().bgCyan.black) //用colors模塊改變輸出顏色
        loadJokes()
    }else{
        console.log('正在加載中~~~'.green)
    }
    rl.prompt()
}).on('close', () => {
    console.log('Bye!')
    process.exit(0)
})

這裏咱們用了一個數組存放笑話,每按下回車,就顯示一條(shift()刪除並返回數組第一個元素),當數組中不足三條時就請求新的一頁

總結

到此,咱們的joke-cli就開發完成。此時咱們應該瞭解了superagent, cheerio, readline等模塊的使用。這會兒應該趁熱打鐵,快去好好看看這些模塊吧

相關連接

你們能夠關注個人公衆號,一塊兒玩耍。有技術乾貨也有扯淡亂談,關注回覆[888]領取福利

JavaScript之禪

左手代碼右手磚,拋磚引玉

相關文章
相關標籤/搜索