node.js来爬取智联全国的竞争最激烈的前十岗位

node爬虫

什么是爬虫呢,是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。为什么选用node呢,因为我是前端,当然要用js实现。

项目分析

爬取http://top.zhaopin.com 智联网站上的全国的竞争最激烈三个月内前十的岗位。不需要定时爬取。使用request和cheerio模块。node版本7.6.0、npm版本4.1.2

安装

npm install request cheerio -S
复制代码

request 模块是一个简化的HTTP客户端。 cheerio 模块专为服务器设计的核心jQuery的快速,灵活和精益的实现。可以把爬到的内容和jQuery一样使用。

核心代码

// app.js
const request = require('request');
const cheerio = require('cheerio');

// 发起请求
request('http://top.zhaopin.com', (error, response, body) => {
    if(error){
        console.error(error);
    }
    let json = {};
    // 获取到的内容放到cheerio模块
    const $ = cheerio.load(body);

    // jQuery 遍历  #hotJobTop .topList li  是通过http://top.zhaopin.com 分析页面结构得到的
    $('#hotJobTop .topList li').each(function (index) {
        let obj = json[index] = {};
        obj.name = $(this).find('.title').text().trim();
        obj.num = $(this).find('.paddingR10').text().trim();
    });

    // 打印数据
    console.log(json);
});
复制代码

执行 node app.js 就会得到如下结果。

[ { name: 'Java开发工程师', num: '340538人/天' },
  { name: '软件工程师', num: '220873人/天' },
  { name: '销售代表', num: '175053人/天' },
  { name: '会计/会计师', num: '168225人/天' },
  { name: '行政专员/助理', num: '150913人/天' },
  { name: 'WEB前端开发', num: '140979人/天' },
  { name: '助理/秘书/文员', num: '139098人/天' },
  { name: '软件测试', num: '136399人/天' },
  { name: '人力资源专员/助理', num: '123482人/天' },
  { name: '用户界面(UI)设计', num: '107505人/天' } ]
复制代码

一个简单的爬虫就写好了,看看前十有没有你从事的岗位吧!

我的博客和github地址

blog.langpz.com

github.com/lanpangzhi

参考

github.com/request/req…

github.com/cheeriojs/c…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值