关注「实验楼」,每天分享一个项目教程
你有没有上班想看笑话却又怕领导发现的经历?现在我们就用几十行代码写一个命令行看笑话段子的小程序,从此无需担心领导的视察。这篇文章和上一篇差不多都是命令行小工具开发,不过本篇更偏向于小爬虫的开发。
我们先来看看我们今天的小目标:
先为它起个命吧:joke-cli
爬取并提取 糗事百科的笑话
输出到命令行按下回车显示一条段子
下面我们将介绍今天用到的主要模块
cheerio
可理解为服务器端的jQuery,基本用法与jQuery一样。有了它,我们在写小爬虫时就可抛开那可爱又可恨的正则表达式了。从此拥抱幸福生活。
用法如下:
let cheerio = require('cheerio')// 将html文本转化成可用jQuery操作的dom,,然后你就可以把它当成jQuery了let $ = cheerio.load('Hello world
')$('h2.title').text('Hello there!')$('h2').addClass('welcome')$.html()//=> Hello there!
更多请参考cheerio
superagent
专注于处理服务端/客户端的http请求,用法如下:
request .get(url) .end(function(err, res){ });
就是这么简答,你已经学会使用它了,别懵逼,这三行代码,已经完全够我们本次示例的使用了。额,当然了,更多的请移步官方文档
superagent
[译] SuperAgent中文使用文档
我们已经介绍了今天的两大主角,那就先来练练手,就用百度吧
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文件中
"bin": { "joke-cli": "./bin/index.js" }
现在执行npm link
。我们的小项目就初始化成功了,就可以认真思考代码了
我们打开糗事百科会发现它的URL还是很简单,由于我们只是爬取段子所以url如下http://www.qiushibaike.com/text/page/2/4965899
,2就是页数。
#!/usr/bin/env nodeconst superAgent = require('superagent')const cheerio = require('cheerio')let url = 'http://www.qiushibaike.com/text/page/'let page = 1superAgent .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 nodeconst 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: '
联系客服