nodejs入门之小爬虫

一、爬虫概念:

网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、自动索引、模拟程序或者蠕虫。

二、基于https 模块实现爬取网页信息

// 1、引入hhtps模块
const https = require('https’)//1.1引入cheerio
const cheerio = require('cheerio’)//2、定义一个请求地址
const url = 'https://www.xxxxxx.com/‘;

https.get(url,(res) => {
	//res是响应对象,我们需要的响应体
	let html = ‘';
	//监听res data事件,响应传输过程中可能会触发多次
	res.on('data',(chunk) => {
	    html += chunk;
	})//监听res end事件,响应体传输完成时触发
	res.on('end',() => {
	    findMenu(html)})
})

const findMenu = (htmlStr) => {
    //1.使用cheerio装载
    var $ = cheerio.load(htmlStr)var $menuMain = $('.menu_main’)// 2.定义一个接收最终结果的数组
    var result = []//3.查找元素
    $menuMain.each((index,item) => {
        var obj = {}var name = $(item).find('h2').text().trim()//console.log(name)
        obj.name = name
        //3.2定义一个存放接收当前大类的二级分类的数组
        obj.subname = []var $as = $(item).find('a’)//3.2循环取出subname
        $as.each((i, aE1) => {
            var aText = $(aE1).text().trim();
            obj.subname.push(aText)})
        result.push(obj)})
    console.log(result)}

最后爬取结果:

[ { name: '技术', subname: [ 'Java', 'PHP', 'C++', '区块链', 'Android', 'iOS', '数据挖掘', '深度学习', '自然语言处理', '机器学习', '测试', 'html5', '技术总监', '架构师', 'CTO' ] },
{ name: '产品', subname: [ '产品总监', '产品经理', '数据产品经理', '游戏策划' ] },
{ name: '设计', subname: [ 'UI设计师', '交互设计', '网页设计师', '平面设计师', '视觉设计师' ] },
{ name: '运营', subname: [ '新媒体运营', '编辑', '数据运营', '运营总监', 'COO' ] },
{ name: '市场', subname: [ '市场营销', '市场推广', '市场策划', '政府关系', '广告文案', '广告投放', '市场推广', 'SEO', 'SEM' ] },
{ name: '销售', subname: [ '销售专员', '销售经理', '销售总监', '大客户代表', '客户经理', '电话销售' ] },
{ name: '职能', subname: [ 'HR', '行政', '财务', '审计' ] },
{ name: '游戏', subname: [ '小游戏开发', 'U3D', '游戏策划', '游戏运营', '剧情设计', '游戏动画', '游戏原画', '游戏运营', '手游推广', '游戏主播', '游戏陪练' ] } ]

三、cheerio 第三方模块

简单理解为是使用在服务器端的 jquery。保留了 jquery 选择器的相关功能,去掉了 DOM 操作功能。

1.安装模块

$ npm install cheerio;

2.引入

const cheerio = require('cheerio’)

3.使用cheerio.load( )装载html字符集,得到一个cheerio对象

const $ = cheerio.load(htmlStr)

4.1 各种类似操作jquery,获取html字符集中h1元素text内容

console.log($(“h1”).tetx())

4.2 给h1元素加个class .class 名为hello

$(“h1”).addClass(“hello”)

4.3 输出html字符串内容

console.log($.html())

接下来再来举个栗子:

从直播网站拉取当时未进直播间人数,未进直播间列表

//引入 http 模块
const http = require('http');
//引入 cheerio
const cheerio = require('cheerio');
//请求的路径
const url = "http://www.yyyyyy.com;

http.get(url,(res) => {
    let raw = ‘';
    //监听res data事件,响应传输过程中可能会触发多次
    res.on('data',(chunk) => {
        raw += chunk;
    });
    //监听res end事件,响应体传输完成时触发
    res.on('end',() => {
        console.log(raw);
    findTbody(raw);
    })
})

const findTbody = (htmlStr) => {
    //1.使用cheerio装载    
    const $ = cheerio.load(htmlStr);
    // 2.定义一个接收最终结果的数组
    let result = [];
    let count = [];
    let nameList = [];
    $('tbody tr').each((index,item) => {
        //过滤掉第一列 列头    
        if(index > 0){
            //总人数
            count++;
            //获取第一个单元格元素
            let oTd1 = $(item).find('td').eq(0);
            //获取第二个单元格元素
            let oTd2 = $(item).find('td').eq(1);
            //拼接成想要的格式  深圳-李淑琳
            var names = $(oTd1).text() + "-" + $(oTd2).text();
            //装进数组
            nameList.push(names);
        }
        result = {
            count:count,
            names:nameList
        }
    })
    var date = new Date();
    var currentTime = date.toLocaleString();
    console.log(`[${currentTime}`);
    console.log("未进直播间人数:" + result.count);
    console.log("未进直播间列表:" + result.names);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值