怎么用node抓取宝可梦图鉴并生成Excel文件

发布时间:2022-08-27 11:11:11 作者:iii
来源:亿速云 阅读:150

怎么用Node抓取宝可梦图鉴并生成Excel文件

引言

宝可梦(Pokémon)是一个全球知名的IP,拥有大量的粉丝和丰富的图鉴数据。对于开发者来说,抓取宝可梦图鉴数据并生成Excel文件是一个有趣且实用的项目。本文将详细介绍如何使用Node.js来实现这一目标。

准备工作

在开始之前,我们需要确保已经安装了Node.js和npm(Node Package Manager)。如果还没有安装,可以从Node.js官网下载并安装。

安装依赖

我们需要安装以下几个npm包:

可以通过以下命令安装这些依赖:

npm install axios cheerio exceljs

抓取宝可梦图鉴数据

1. 确定目标网站

首先,我们需要确定一个包含宝可梦图鉴数据的网站。以宝可梦图鉴为例,这个网站提供了详细的宝可梦信息。

2. 发送HTTP请求

使用axios发送HTTP请求,获取网页的HTML内容。

const axios = require('axios');

async function fetchPokemonData() {
    try {
        const response = await axios.get('https://www.pokemon.com/us/pokedex/');
        return response.data;
    } catch (error) {
        console.error('Error fetching Pokemon data:', error);
    }
}

3. 解析HTML内容

使用cheerio解析HTML内容,提取所需的宝可梦信息。

const cheerio = require('cheerio');

async function parsePokemonData(html) {
    const $ = cheerio.load(html);
    const pokemonList = [];

    $('.pokemon-info').each((index, element) => {
        const name = $(element).find('.pokemon-name').text().trim();
        const number = $(element).find('.pokemon-number').text().trim();
        const type = $(element).find('.pokemon-type').text().trim();
        const description = $(element).find('.pokemon-description').text().trim();

        pokemonList.push({
            name,
            number,
            type,
            description
        });
    });

    return pokemonList;
}

4. 抓取并解析数据

将上述两个函数结合起来,抓取并解析宝可梦图鉴数据。

async function getPokemonData() {
    const html = await fetchPokemonData();
    const pokemonList = await parsePokemonData(html);
    return pokemonList;
}

生成Excel文件

1. 创建Excel工作簿

使用exceljs创建一个新的Excel工作簿。

const ExcelJS = require('exceljs');

async function createExcelFile(pokemonList) {
    const workbook = new ExcelJS.Workbook();
    const worksheet = workbook.addWorksheet('Pokemon');

    // 设置表头
    worksheet.columns = [
        { header: '编号', key: 'number', width: 10 },
        { header: '名称', key: 'name', width: 20 },
        { header: '类型', key: 'type', width: 15 },
        { header: '描述', key: 'description', width: 50 }
    ];

    // 添加数据
    pokemonList.forEach(pokemon => {
        worksheet.addRow(pokemon);
    });

    // 保存文件
    await workbook.xlsx.writeFile('pokemon.xlsx');
    console.log('Excel文件已生成');
}

2. 生成Excel文件

将抓取到的宝可梦数据生成Excel文件。

async function main() {
    const pokemonList = await getPokemonData();
    await createExcelFile(pokemonList);
}

main();

完整代码

以下是完整的代码示例:

const axios = require('axios');
const cheerio = require('cheerio');
const ExcelJS = require('exceljs');

async function fetchPokemonData() {
    try {
        const response = await axios.get('https://www.pokemon.com/us/pokedex/');
        return response.data;
    } catch (error) {
        console.error('Error fetching Pokemon data:', error);
    }
}

async function parsePokemonData(html) {
    const $ = cheerio.load(html);
    const pokemonList = [];

    $('.pokemon-info').each((index, element) => {
        const name = $(element).find('.pokemon-name').text().trim();
        const number = $(element).find('.pokemon-number').text().trim();
        const type = $(element).find('.pokemon-type').text().trim();
        const description = $(element).find('.pokemon-description').text().trim();

        pokemonList.push({
            name,
            number,
            type,
            description
        });
    });

    return pokemonList;
}

async function getPokemonData() {
    const html = await fetchPokemonData();
    const pokemonList = await parsePokemonData(html);
    return pokemonList;
}

async function createExcelFile(pokemonList) {
    const workbook = new ExcelJS.Workbook();
    const worksheet = workbook.addWorksheet('Pokemon');

    // 设置表头
    worksheet.columns = [
        { header: '编号', key: 'number', width: 10 },
        { header: '名称', key: 'name', width: 20 },
        { header: '类型', key: 'type', width: 15 },
        { header: '描述', key: 'description', width: 50 }
    ];

    // 添加数据
    pokemonList.forEach(pokemon => {
        worksheet.addRow(pokemon);
    });

    // 保存文件
    await workbook.xlsx.writeFile('pokemon.xlsx');
    console.log('Excel文件已生成');
}

async function main() {
    const pokemonList = await getPokemonData();
    await createExcelFile(pokemonList);
}

main();

结论

通过本文的介绍,我们学习了如何使用Node.js抓取宝可梦图鉴数据并生成Excel文件。这个过程涉及到了HTTP请求、HTML解析、数据提取和Excel文件生成等多个步骤。希望这篇文章能够帮助你更好地理解如何使用Node.js进行数据抓取和处理。

参考资料

推荐阅读:
  1. 如何使用node读写Excel文件
  2. 怎么在nodejs中利用node-xlsx生成一个excel文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

node excel

上一篇:vue跟小程序的区别有哪些

下一篇:mysql存储引擎之间的区别有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》