当前位置:   article > 正文

node爬虫,抓取网页数据_node 爬虫

node 爬虫

node爬虫,抓取网页数据

1、什么是爬虫?
  • 抓取信息或者数据的程序或者是脚本
2、通过node实现对网页数据的抓取.
  • 安装插件
  • request,处理请求(此包以被弃用)
npm i request -D
  • 1
  • iconv-lite,解决编码格式
npm i iconv-lite -D
  • 1
  • cheerio,同jQuery 可以使用$选择器
npm i cheerio -D
  • 1
  • 对某讯页面数据的抓取,新建index.js
const fs = require('fs');
const iconv = require("iconv-lite");//解决编码格式
const request = require("request");//处理请求
const cheerio = require("cheerio");//同jQuery 可以使用$选择器

request('https://v.qq.com/biu/ranks/?t=hotplay&channel=all#anime', { encoding: null }, function (error, response, body) {
    if (error) throw error;

    console.log('爬取成功...')

    let str = iconv.decode(body, 'utf-8')//bufer转码  请求过来的网页

    // console.log(str)

    const $ = cheerio.load(str);

    let arr = [];

    //分析dom结构 抓取数据
    for (let i = 1; i <= 2; ++i) {

        for (let j = 1; j <= 3; ++j) {

            let selImg1 = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hot_first > a > img`
            let selHref1 = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hot_first > a`
            let selText = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hot_first > strong`
            let selDes = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hot_first > div.figure_desc`

            let obj = {
                imgSrc: $(selImg1).attr('src'),
                href: $(selHref1).attr('href'),
                text: $(selText).text(),
                des: $(selDes).text(),
                brother: [],
            }
            for (let k = 1; k <= 4; ++k) {
                let broSelImg1 = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hotlist > ol > li:nth-child(${k}) > a > img`
                let broSelHref1 = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hotlist > ol > li:nth-child(${k}) > a`
                let broSelText1 = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hotlist > ol > li:nth-child(${k}) > strong`
                let broDes = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hotlist > ol > li:nth-child(${k}) > div`

                let broObj = {
                    imgSrc: $(broSelImg1).attr('src'),
                    href: $(broSelHref1).attr('href'),
                    text: $(broSelText1).text(),
                    des: $(broDes).text()
                }

                obj.brother.push(broObj)

            }
            arr.push(obj)
        }

    };

    //将抓取的数据以json格式写入本地
    fs.writeFile('./pubilc/index.json', JSON.stringify(arr), (err) => {
        if (err) throw err;
        console.log('json文件写入成功...')
    })


    //将网页写入本地
    fs.writeFile('./pubilc/index.html', str, (err) => {
        if (err) throw err;
        console.log('html文件写入成功...')
    })

});

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

在这里插入图片描述

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/848658
推荐阅读
相关标签
  

闽ICP备14008679号