superagent 抓取网页 cheerio 分析网页

来源:互联网 发布:人工智能会消灭人类 编辑:程序博客网 时间:2024/05/17 07:48

  1. 学习使用 superagent 抓取网页
  2. 学习使用 cheerio 分析网页

superagent(http://visionmedia.github.io/superagent/ ) 是个 http 方面的库,可以发起 get 或 post 请求。

cheerio(https://github.com/cheeriojs/cheerio ) 大家可以理解成一个 Node.js 版的 jquery,用来从网页中以 css selector 取数据,使用方式跟 jquery 一样一样的。



SuperAgent —— Node 的 客户端请求


var request = require('superagent');
request   .post('/api/pet')   .send({ name: 'Manny', species: 'cat' })   .set('X-API-Key', 'foobar')   .set('Accept', 'application/json')   .end(function(err, res){     if (res.ok) {       alert('yay got ' + JSON.stringify(res.body));     } else {       alert('Oh no! error ' + res.text);     }   });

SuperAgent请求的默认方法为GET,所以你可可以简单地写如下代码:

request('/search', function(res){ });


0 0