Python王者荣耀英雄皮肤批量下载
一、使用requests批量下载
第一步,找到各个英雄皮肤的所在位置
第二步,获取到皮肤的url
第三步,下载
1、查找英雄皮肤所在位置
-
进入王者荣耀英雄资料的官网,在该页面下可查看到所有英雄。
-
点击任意一个英雄,进入单个英雄页面介绍,在右下角可看到该英雄的皮肤。
2、获取皮肤的url
1)单个英雄皮肤的url
接上文,进入审查模式F12,鼠标移动到任一皮肤位置处,可查看到皮肤地址
https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/155/155-bigskin-2.jpg
https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/155/155-bigskin-3.jpg
以下是刚获取到皮肤的url,可以看到该英雄的皮肤url只有最后的一个数字是变量,因此推断该数字为皮肤的序号,只需要改变该数字则可得到该英雄的不同皮肤。
- https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/155/155-bigskin-2.jpg
- https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/155/155-bigskin-3.jpg
在该英雄的页面看到有三个皮肤(包括原皮),因此原皮的url为:
https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/155/155-bigskin-1.jpg
2)英雄页面的url
现在已经知道了单个英雄皮肤的url变化,但各个英雄皮肤的url肯定是不同的。接下来换一个英雄查看一下。可以看到安琪拉心灵骇客皮肤的url为:
http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/142/142-bigskin-4.jpg
由以上获取到的链接发现,艾琳皮肤url中的155在安琪拉皮肤的url中变成了142,因此猜测这些数字为英雄的ID。现在,已经得到了皮肤url构成方式、单个英雄页面url的构造方式(加粗部分为变量);还需要各个英雄的ID。
- 英雄页面url的构造方式:https://pvp.qq.com/web201605/herodetail/155.shtml
- 皮肤url构成方式:https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/155/155-bigskin-3.jpg
3)英雄ID的获取
在官网页面下按F12审查元素,筛查后可以发下在herolist.json中有英雄的ID;其url为:
https://pvp.qq.com/web201605/js/herolist.json
https://game.gtimg.cn/images/yxzj/web201706/js/heroid.js #该地址也可以获得英雄的ID,不过在现在的网页中我没有筛查到,但目前也可以用。
3、敲代码下载皮肤
1)获取所有英雄的ID
#获取所有英雄id
def get_id():
id_list = []#存放英雄的id
#url of the hero id
id_url = 'https://pvp.qq.com/web201605/js/herolist.json'
id_str = requests.get(id_url,camouflage_browser())#camouflage_browser():调用伪装浏览器的函数
# print(id_str.text)
print(type(id_str.text))#此时类型为str
dict_id = json.loads(id_str.text)#加载成字典类型
# print(dict_id)
print(type(dict_id[0]))
#遍历dict_id
for ename in dict_id:
id = ename["ename"]#英雄ID
name = ename["cname"]#英雄名
id_list.append({name:id})
print(id_list)
以上代码运行结果为:
<class 'str'>
<class 'dict'>
[{'name': '廉颇', 'id': 105}, {'name': '小乔', 'id': 106}, {'name': '赵云', 'id': 107}, {'name': '墨子', 'id': 108}, {'name': '妲己', 'id': 109}, {'name': '嬴政', 'id': 110}, {'name': '孙尚香', 'id': 111}, {'name': '鲁班七号', 'id': 112}, {'name': '庄周', 'id': 113}, {'name': '刘禅', 'id': 114}, {'name': '高渐离', 'id': 115}, {'name': '阿轲', 'id': 116}, {'name': '钟无艳', 'id': 117}, {'name': '孙膑', 'id': 118}, {'name': '扁鹊', 'id': 119}, {'name': '白起', 'id': 120}, {'name': '芈月', 'id': 121}, {'name': '吕布', 'id': 123}, {'name': '周瑜', 'id': 124}, {'name': '夏侯惇', 'id': 126}, {'name': '甄姬', 'id': 127}, {'name': '曹操', 'id': 128}, {'name': '典韦', 'id': 129}, {'name': '宫本武藏', 'id': 130}, {'name': '李白', 'id': 131}, {'name': '马可波罗', 'id': 132}, {'name': '狄仁杰', 'id': 133}, {'name': '达摩', 'id': 134}, {'name': '项羽', 'id': 135}, {'name': '武则天', 'id': 136}, {'name': '老夫子', 'id': 139}, {'name': '关羽', 'id': 140}, {'name': '貂蝉', 'id': 141}, {'name': '安琪拉', 'id': 142}, {'name': '程咬金', 'id': 144}, {'name': '露娜', 'id': 146}, {'name': '姜子牙', 'id': 148}, {'name': '刘邦', 'id': 149}, {'name': '韩信', 'id': 150}, {'name': '王昭君', 'id': 152}, {'name': '兰陵王', 'id': 153}, {'name': '花木兰', 'id': 154}, {'name': '张良', 'id': 156}, {'name': '不知火舞', 'id': 157}, {'name': '娜可露露', 'id': 162}, {'name': '橘右京', 'id': 163}, {'name': '亚瑟', 'id': 166}, {'name': '孙悟空', 'id': 167}, {'name': '牛魔', 'id': 168}, {'name': '后羿', 'id': 169}, {'name': '刘备', 'id': 170}, {'name': '张飞', 'id': 171}, {'name': '李元芳', 'id': 173}, {'name': '虞姬', 'id': 174}, {'name': '钟馗', 'id': 175}, {'name': '成吉思汗', 'id': 177}, {'name': '杨戬', 'id': 178}, {'name': '雅典娜', 'id': 183}, {'name': '蔡文姬', 'id': 184}, {'name': '太乙真人', 'id': 186}, {'name': '哪吒', 'id': 180}, {'name': '诸葛亮', 'id': 190}, {'name': '黄忠', 'id': 192}, {'name': '大乔', 'id': 191}, {'name': '东皇太一', 'id': 187}, {'name': '干将莫邪', 'id': 182}, {'name': '鬼谷子', 'id': 189}, {'name': '铠', 'id': 193}, {'name': '百里守约', 'id': 196}, {'name': '百里玄策', 'id': 195}, {'name': '苏烈', 'id': 194}, {'name': '梦奇', 'id': 198}, {'name': '女娲', 'id': 179}, {'name': '明世隐', 'id': 501}, {'name': '公孙离', 'id': 199}, {'name': '杨玉环', 'id': 176}, {'name': '裴擒虎', 'id': 502}, {'name': '弈星', 'id': 197}, {'name': '狂铁', 'id': 503}, {'name': '米莱狄', 'id': 504}, {'name': '元歌', 'id': 125}, {'name': '孙策', 'id': 510}, {'name': '司马懿', 'id': 137}, {'name': '盾山', 'id': 509}, {'name': '伽罗', 'id': 508}, {'name': '沈梦溪', 'id': 312}, {'name': '李信', 'id': 507}, {'name': '上官婉儿', 'id': 513}, {'name': '嫦娥', 'id': 515}, {'name': '猪八戒', 'id': 511}, {'name': '盘古', 'id': 529}, {'name': '瑶', 'id': 505}, {'name': '云中君', 'id': 506}, {'name': '曜', 'id': 522}, {'name': '马超', 'id': 518}, {'name': '西施', 'id': 523}, {'name': '鲁班大师', 'id': 525}, {'name': '蒙犽', 'id': 524}, {'name': '镜', 'id': 531}, {'name': '蒙恬', 'id': 527}, {'name': '阿古朵', 'id': 533}, {'name': '夏洛特', 'id': 536}, {'name': '澜', 'id': 528}, {'name': '司空震', 'id': 537}, {'name': '艾琳', 'id': 155}, {'name': '云缨', 'id': 538}, {'name': '金蝉', 'id': 540}, {'name': '暃', 'id': 542}, {'name': '桑启', 'id': 534}, {'name': '戈娅', 'id': 548}, {'name': '海月', 'id': 521}][{'廉颇': 105}, {'小乔': 106}, {'赵云': 107}, {'墨子': 108}, {'妲己': 109}, {'嬴政': 110}, {'孙尚香': 111}, {'鲁班七号': 112}, {'庄周': 113}, {'刘禅': 114}, {'高渐离': 115}, {'阿轲': 116}, {'钟无艳': 117}, {'孙膑': 118}, {'扁鹊': 119}, {'白起': 120}, {'芈月': 121}, {'吕布': 123}, {'周瑜': 124}, {'夏侯惇': 126}, {'甄姬': 127}, {'曹操': 128}, {'典韦': 129}, {'宫本武藏': 130}, {'李白': 131}, {'马可波罗': 132}, {'狄仁杰': 133}, {'达摩': 134}, {'项羽': 135}, {'武则天': 136}, {'老夫子': 139}, {'关羽': 140}, {'貂蝉': 141}, {'安琪拉': 142}, {'程咬金': 144}, {'露娜': 146}, {'姜子牙': 148}, {'刘邦': 149}, {'韩信': 150}, {'王昭君': 152}, {'兰陵王': 153}, {'花木兰': 154}, {'张良': 156}, {'不知火舞': 157}, {'娜可露露': 162}, {'橘右京': 163}, {'亚瑟': 166}, {'孙悟空': 167}, {'牛魔': 168}, {'后羿': 169}, {'刘备': 170}, {'张飞': 171}, {'李元芳': 173}, {'虞姬': 174}, {'钟馗': 175}, {'成吉思汗': 177}, {'杨戬': 178}, {'雅典娜': 183}, {'蔡文姬': 184}, {'太乙真人': 186}, {'哪吒': 180}, {'诸葛亮': 190}, {'黄忠': 192}, {'大乔': 191}, {'东皇太一': 187}, {'干将莫邪': 182}, {'鬼谷子': 189}, {'铠': 193}, {'百里守约': 196}, {'百里玄策': 195}, {'苏烈': 194}, {'梦奇': 198}, {'女娲': 179}, {'明世隐': 501}, {'公孙离': 199}, {'杨玉环': 176}, {'裴擒虎': 502}, {'弈星': 197}, {'狂铁': 503}, {'米莱狄': 504}, {'元歌': 125}, {'孙策': 510}, {'司马懿': 137}, {'盾山': 509}, {'伽罗': 508}, {'沈梦溪': 312}, {'李信': 507}, {'上官婉儿': 513}, {'嫦娥': 515}, {'猪八戒': 511}, {'盘古': 529}, {'瑶': 505}, {'云中君': 506}, {'曜': 522}, {'马超': 518}, {'西施': 523}, {'鲁班大师': 525}, {'蒙犽': 524}, {'镜': 531}, {'蒙恬': 527}, {'阿古朵': 533}, {'夏洛特': 536}, {'澜': 528}, {'司空震': 537}, {'艾琳': 155}, {'云缨': 538}, {'金蝉': 540}, {'暃': 542}, {'桑启': 534}, {'戈娅': 548}, {'海月': 521}]
2)访问各个英雄的页面
#访问各个英雄url,获取每个英雄共有几个皮肤
def visit_hero_url():
id_list = get_id()#调用英雄id的函数
for hero_id in id_list:
# url of hero
hero_url = 'https://pvp.qq.com/web201605/herodetail/{}.shtml'.format(hero_id["id"])
print("The url of "+hero_id["name"]+" is "+hero_url)
html = requests.get(hero_url,camouflage_browser())
pattern = 'pic-pf-list pic-pf-list3" data-imgname=(.*?)>'
skins = re.findall(pattern,html.content.decode("gbk"))[0]#查看原网页后charset="gbk",因此需要进行转码
skins = re.sub("&|[0-9]{0,5}|\"","",skins)
for index,name in enumerate(skins.split("|")):#遍历英雄所有皮肤
'''
index:皮肤的序号
name:皮肤名称
'''
print(index,name)
运行结果:
The url of 廉颇 is https://pvp.qq.com/web201605/herodetail/105.shtml
0 正义爆轰
1 地狱岩魂
2 无尽征程
3 寅虎·御盾
3)下载
#下载皮肤
def download_skin(hero_id,index,name):
#The URL of the hero skin
skin_url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{}/{}-bigskin-{}.jpg'.format(hero_id,hero_id,index)
print("The url of skin is "+skin_url)
pic_html = requests.get(skin_url,camouflage_browser())#To visit the url of picture
# time.sleep(0.5)
with open(name+".jpg","wb") as f:
f.write(pic_html.content)
4)完整代码
# -*- coding:utf-8 -*-
import requests
import random
import json
import re
import time
def camouflage_browser():
ur1 = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1'
ur2 = 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36'
ur3 = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE/10.0.2410.0'
ur4 = 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36'
ur5 = 'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
ur6 = 'Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.43 Mobile Safari/537.36'
ur7 = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.43 Safari/537.36'
ur8 = 'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1'
ur9 = 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'
ur10 = 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
ur11 = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
ur12 = 'Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1'
ur_list = [ur1, ur2, ur3, ur4, ur5, ur6, ur7, ur8, ur9, ur10, ur11, ur12]
headers = {
'user-agent': random.choice(ur_list)
}
return headers
#Get the hero ID
def get_id():
id_list = []#存放英雄的id
#url of the hero id
id_url = 'https://pvp.qq.com/web201605/js/herolist.json'
id_str = requests.get(id_url,camouflage_browser())
# print(id_str.text)
# print(type(id_str.text))#此时类型为str
dict_id = json.loads(id_str.text)#加载成字典类型
# print(dict_id)
# print(type(dict_id[0]))
#遍历dict_id
for ename in dict_id:
id = ename["ename"]#英雄ID
name = ename["cname"]#英雄名
id_list.append({"name":name,"id":id})
# print(id_list)
return id_list
#访问各个英雄url,获取共有几个皮肤
def visit_hero_url():
id_list = get_id()#调用英雄id的函数
for hero_id in id_list:
# url of hero
hero_url = 'https://pvp.qq.com/web201605/herodetail/{}.shtml'.format(hero_id["id"])
print("The url of "+hero_id["name"]+" is "+hero_url)
html = requests.get(hero_url,camouflage_browser())
pattern = 'pic-pf-list pic-pf-list3" data-imgname=(.*?)>'
skins = re.findall(pattern,html.content.decode("gbk"))[0]#查看原网页后charset="gbk",因此需要进行转码
skins = re.sub("&|[0-9]{0,5}|\"","",skins)
for index,name in enumerate(skins.split("|")):#遍历英雄所有皮肤
'''
index:皮肤的序号
name:皮肤名称
'''
print(index,name)
download_skin(hero_id["id"],index+1,name)#传递英雄ID,皮肤序号,皮肤名称
#下载皮肤
def download_skin(hero_id,index,name):
#The URL of the hero skin
skin_url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{}/{}-bigskin-{}.jpg'.format(hero_id,hero_id,index)
print("The url of skin is "+skin_url)
#To visit the url of picture
pic_html = requests.get(skin_url,camouflage_browser())
time.sleep(0.5)
with open(name+".jpg","wb") as f:
f.write(pic_html.content)
if __name__ == '__main__':
visit_hero_url()
运行结果: