node抓取网页数据

313 阅读1分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第12天,[点击查看活动详情]

node爬虫,抓取网页数据

1、什么是爬虫?

抓取信息或者数据的程序或者是脚本

2、通过node实现对网页数据的抓取.

安装插件 request,处理请求(此包以被弃用)

npm i request -D

iconv-lite,解决编码格式

npm i iconv-lite -D

cheerio,同jQuery 可以使用$选择器

npm i cheerio -D

对某讯页面数据的抓取,新建index.js const fs = require('fs'); const iconv = require("iconv-lite");//解决编码格式 const request = require("request");//处理请求 const cheerio = require("cheerio");//同jQuery 可以使用$选择器

request('v.qq.com/biu/ranks/?…', { 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文件写入成功...')
})