前端小白,因为想找一份前端开发的工作,所以想用 node.js 做一个爬虫,爬取拉勾网上的招聘职位的信息,再做一个在线图形化的展示,现在只能通过手动修改 pn 得到某一页的 json 数据。所以想请教一下大家,通过什么方法能把所有页面自动遍历一遍呢,然后得到的 json 数据通过什么方式存起来?现在只能单纯的 console.log(),是需要用数据库吗?数据库的内容又怎么拿出来呢,而且本地数据库的信息好像也没办法做成在线的网页展示呀?
var http = require('http');
var url = 'http://www.lagou.com/jobs/positionAjax.jsonpx=default&first=true&city=%E5%8C%97%E4%BA%AC&pn=2&kd=%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91'
var jobData = [];
http.get(url, function(res) {
var html = '';
res.on('data', function(data){
html += data;
});
res.on('end', function() {
var json = JSON.parse(html);
var aResult = json.content.positionResult.result;
aResult.forEach(function (item) {
// console.log(item.companyName);
// console.log(item.salary);
// console.log(item.workYear+'\n');
jobData.push({'companyName': item.companyName, 'salary': item.salary, 'workYear': item.workYear});
});
console.log(jobData);
});
}).on('error', function() {
console.log('获取失败');
});