04_简单的爬虫cheerio基于node.js

基于node.js

1、 先去安装 cheerio模块

2、且要获取http 或者 https

3、var url = "http//:www.news/baidu.com"

4、通过 http.get()去获取

http.get(url,function(response){

// 4.1开始爬数据

var str =""; //(存储数据)

response.on("data",function(chunk){

str+=chunk;

})

//4.2、结束

response.on("end",function(){

// 为实例化渲染数据 cheerio

// decodeEntities:false  解码

var $ = cheerio.load(str, decodeEntities:false);

var arr = []; //真的数据

//(ul li)去查看你要想数据的class或者id名,用法同jq

$("ul li ").each(function(){

arr.push({

title:$(this).find('.gname').html(),

img:$(this).find('a').html(),

xj: $(this).find(".xj b").html(),

yj:$(this).find(".yj em").html()

});

})

console.log(arr); //打印出自己的

可以在这里去写 在mongoDB插入数据的代码第四步4

就可以把爬到的数据放到数据库之中

})

})

文章同步上一片

你可能感兴趣的:(04_简单的爬虫cheerio基于node.js)