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

5次阅读

共计 1280 个字符,预计需要花费 4 分钟才能阅读完成。

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 地址

http://blog.langpz.com

https://github.com/lanpangzhi

参考

https://github.com/request/request

https://github.com/cheeriojs/cheerio

正文完
 0