用Python爬取"王者农药"英雄皮肤
王者荣耀
0.引言
作为一款现象级游戏,王者荣耀,想必大家都玩过或听过,游戏里中各式各样的英雄,每款皮肤都非常精美,用做电脑壁纸再合适不过了。本篇就来教大家如何使用Python来爬取这些精美的英雄皮肤。
1.环境
操作系统:Windows / Linux
Python版本:3.7.2
2.需求分析
我们打开《王者荣耀》官网,找定位到英雄列表的页面
可直接点此链接:
https://pvp.qq.com/web201605/herolist.shtml 。
用Python爬取"王者农药"英雄皮肤王者荣耀
在这个网页中包含了所有的英雄,头像及英雄名称。点击其中一个英雄的头像,如“嫦娥”,进去后如下图:
用Python爬取"王者农药"英雄皮肤王者荣耀
我们记下此时的网址。
<pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">https://pvp.qq.com/web201605/herodetail/515.shtml
</pre>
再后退到英雄列表页面,点“甄姬”进去查看:
<pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">https://pvp.qq.com/web201605/herodetail/127.shtml
</pre>
可以看到这些网址几乎是固定不变的,变化的只是515、127这些数字,这些其实就是代表的英雄数字编号。
那么第一个关键点就来了,怎么找出各个英雄所对应的数字编号呢?
我们回到最初的英雄列表页面,打开浏览器的开发者工具<F12>,刷新页面、仔细观察,你会找到一个herolist.json的文件,如图所示:
用Python爬取"王者农药"英雄皮肤python爬虫
这里记录了各个英雄的信息,其中就包含了每个英雄对应的数字编号了,请忽略这里截图中的乱码显示。我们切到herolist.json中的Headers,就可以拿到该请求的URL地址,进而就可以把英雄及其对应的数字,编号都提取出来了。
有了英雄编号的对应关系,再找寻下英雄皮肤的链接规律。
现在重新进入一个英雄的网址,打开浏览器的开发者工具,刷新页面,在Network下刷新并找到英雄的皮肤图片,如图所示:
用Python爬取"王者农药"英雄皮肤python爬虫
在Headers中查看该图片的网址,查看即Request URL处的链接:
https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/515/515-bigskin-1.jpg
找寻一个看看
https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/529/529-bigskin-1.jpg
继续寻一个看看
https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/127/127-bigskin-4.jpg
仔细分析如上三个链接,我们可以把英雄皮肤的URL拆分开来看。它是由一个固定前缀(我们可以记为base_url),再加上英雄数字编号、"bigskin"、皮肤编号、".jpg"组合而成,如下:
base_url / hero_num / hero_num - bigskin - heroskin_num .jpg
拿到了各个英雄皮肤的URL地址后,我们就可以进行图片的下载并保存在本地了。
3.代码演示
首先导入我们所用到的模块
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import requests
import os
</pre>
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 注:requests是非内置模块,若环境中没有,需自行安装:
pip install requests
</pre>
3.1 提取英雄名字及数字
使用herolist.json拿到herolist,并提取出我们关心的内容
用Python爬取"王者农药"英雄皮肤python编程
3.2 构造英雄皮肤的URL
首先准备好我们的BASE_URL,即英雄皮肤的固定前缀。
h_l='http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'
接下来构造好英雄皮肤的URL,同时我们需要对每一个英雄的所有皮肤进行遍历,如下:
用Python爬取"王者农药"英雄皮肤python编程
3.3 存储图片
最后我们就只需将获取到的图片保存在本地即可。
用Python爬取"王者农药"英雄皮肤python编程
4.效果展示
最终的爬取效果如下图所示。
用Python爬取"王者农药"英雄皮肤王者荣耀
5.总结
短短几十行代码就可以把心爱英雄的精美皮肤保存下来,赶快实操起来吧!
加群:835017344,获取本文全套代码!