基于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開始爬數(shù)據(jù)
var str =""; //(存儲數(shù)據(jù))
response.on("data",function(chunk){
str+=chunk;
})
//4.2、結(jié)束
response.on("end",function(){
// 為實例化渲染數(shù)據(jù) cheerio
// decodeEntities:false? 解碼
var $ = cheerio.load(str, decodeEntities:false);
var arr = []; //真的數(shù)據(jù)
//(ul li)去查看你要想數(shù)據(jù)的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插入數(shù)據(jù)的代碼第四步4
就可以把爬到的數(shù)據(jù)放到數(shù)據(jù)庫之中
})
})
文章同步上一片