# 王者皮肤图片爬虫 **Repository Path**: dttx123/king_skin_picture_reptile ## Basic Information - **Project Name**: 王者皮肤图片爬虫 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-02-24 - **Last Updated**: 2021-10-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 王者荣耀 皮肤图片爬虫 ## 基础分析: > 在王者官网, 分析ajax 请求返回的 数据如下: ![1582536419450](./img/1582536419450.png) ```js [{ "ename": 105, "cname": "廉颇", "title": "正义爆轰", "new_type": 0, "hero_type": 3, "skin_name": "正义爆轰|地狱岩魂" }, { "ename": 106, "cname": "小乔", "title": "恋之微风", "new_type": 0, "hero_type": 2, "skin_name": "恋之微风|万圣前夜|天鹅之梦|纯白花嫁|缤纷独角兽|丁香结" } /* "ename": 英雄编号 "cname": 姓名名称 "title": 描述 "new_type": 0, "hero_type": 英雄分类 "skin_name": 英雄皮肤数组 */ ``` ## 代码实现爬取图片 ```js const https = require('https'); const fs = require('fs'); const path = require('path'); let heroList = null; // 存放英雄数据的数组 // 读取英雄数据 fs.readFile(path.join(__dirname, 'data/herolist.json'), (err, data) => { data = JSON.parse(data); heroList = data; // 获取英雄数据 // 遍历英雄数据 heroList.forEach((v, index) => { // 给当前英雄创建对应的文件夹 fs.mkdir(`./images/${v.cname}`, () => console.log(`${v.cname} 创建完成`)); // 获取当前英雄皮肤的个数 let length = v.skin_name.split('|').length; //定时器延迟, 同时发送大量请求, 回到请求丢失, 服务器无法响应全部数据,遂延迟 // 避免瞬间 请求过多 setTimeout(() => { for (let i = 1; i <= length; i++) { // 下载当前英雄的第i张图片 getImage(v, i); } }, index * 1500); }) }) // 爬图片 的方法 // item 当前英雄形式 // index 第几张图片 function getImage (item, index) { // 动态拼接 英雄皮肤的图片地址 let url = `https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/${item.ename}/${item.ename}-bigskin-${index}.jpg`; //创建url对象 const options = new URL(url); const req = https.request(options, (res) => { let imgData = ""; //存放图片片段的容器 res.setEncoding("binary"); //一定要设置response的编码为binary否则会下载下来的图片打不开 res.on("data", function (chunk) { imgData += chunk; }); // 接收完成后,以进制的形式保存图片 res.on("end", function () { fs.writeFile(`./images/${item.cname}/${index}.jpg`, imgData, "binary", function (err) { if (err) { console.log(`图片${item.cname}-${index}.jpg保存失败`); } console.log(`图片${item.cname}-${index}.jpg保存成功`); }); }); }); req.on('error', (e) => { console.error('请求出错'); }); req.end(); } ``` ## 查看结果 ![1582537215969](./img/1582537215969.png)