你有没有上班想看笑话却又怕领导发现的经历?如今咱们就用几十行代码写一个命令行看笑话段子的小程序,今后无需担忧领导的视察。这篇文章和上一篇差很少都是命令行小工具开发,不过本篇更偏向于小爬虫的开发
咱们先来看看咱们今天的小目标:javascript
joke-cli
下面咱们将介绍今天用到的主要模块html
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
专一于处理服务端/客户端的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以下http://www.qiushibaike.com/text/page/2/4965899
,2就是页数。
#!/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结够后,如今能够用咱们用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模块
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中文网
#!/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]领取福利
左手代码右手砖,抛砖引玉