package.json文件:

NodeJs+Request+Cheerio 采集数据
{
"name":"zqz",
"version":"1.0.1",
"private":false,
"dependencies":{
"request":"*",
"cheerio":"*"
}
}
NodeJs+Request+Cheerio 采集数据

cdm中执行:npm install 进行安装依赖的2个包。

 

app.js文件:

NodeJs+Request+Cheerio 采集数据
/**
* 数据采集
*/
//引入需要的包
var request = require('request');
var cheerio = require('cheerio');

//定义常量
var dolphin = 'http://cn.dolphin.com/blog';

//数据请求
function dataRequest(dataUrl) {
//发送请求
request({
url : dataUrl,
method : 'GET'
},function(err, red, body) {
//请求到body
if(err){
console.log(dataUrl);
console.error('[ERROR]Collection' + err);
return;
}

if(dataUrl && dataUrl === dolphin){
dataPraseDolphin(body);
}
})
}

/**
* 解析html
*/
function dataPraseDolphin(body) {

var $ = cheerio.load(body);

var atricles = $('#content').children('.status-publish');

for(var i = 0;i < atricles.length;i++){
var article = atricles[i];

var $a = $(article).find('.post-title .entry-title a');
var $p = $(article).find('.post-content p');

var $aVal = $($a).text();
var $pVal = $($p).text();

if($p)
{
console.info('--------------'+ (i+1) +' Chapter------------------');
console.info('标题:' + $aVal);
console.info('简介:' + $pVal);
console.info('时间:' + new Date)
console.info('---------------------------------------------------');
}
}
}

//开始发送请求 并 采集数据
dataRequest(dolphin);
NodeJs+Request+Cheerio 采集数据

 

Sublime 中 ctrl+B 执行

结果:NodeJs+Request+Cheerio 采集数据

 转载:http://www.cnblogs.com/zqzjs/p/5487348.html

相关文章:

  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2022-01-02
猜你喜欢
  • 2021-09-08
  • 2021-06-27
  • 2021-08-31
  • 2021-11-22
  • 2021-04-05
相关资源
相似解决方案