爬虫获取王者荣耀全英雄皮肤
# coding=utf-8
import os,requests,shutil
"""
王者荣耀全英雄皮肤
"""
def getHeroListInfo(url=u'https://pvp.qq.com/web201605/js/herolist.json'):
"""
获取到英雄列表信息
:param url:
:return:hero_name_list,hero_number_list
"""
herolist = requests.get(url) # 获取英雄列表json文件
herolist_json = herolist.json() # 转化为json格式
hero_name_list = list(map(lambda x: x['cname'], herolist.json())) # 提取英雄的名字
hero_number_list = list(map(lambda x: x['ename'], herolist.json())) # 提取英雄的编号
return hero_name_list, hero_number_list
# 下载图片
def downloadPic(storageDir="data",
downloadUrl=u'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/',
max_num = 20):
"""
下载图片
:param storageDir: 储存目录
:param downloadUrl:下载路径
:param max_num: 最大皮肤种类个数
:return:
"""
#记录当前目录
current_dir = os.getcwd()
if not os.path.exists(storageDir):
os.mkdir(storageDir)
else:
shutil.rmtree(storageDir)
os.mkdir(storageDir)
print("[INFO]: 储存目录已清空", os.path.join(current_dir,storageDir))
hero_name_list, hero_number_list = getHeroListInfo()
print("[INFO]: 已获取到英雄列表信息:\n",
hero_name_list,
hero_number_list)
i = 0
for hero_number in hero_number_list:
hero_name = hero_name_list[i]
print("[INFO]: 获取",hero_name,"英雄图片中...")
#恢复目录
os.chdir(current_dir)
hero_dir = os.path.join(storageDir, hero_name)
# 创建文件夹
if not os.path.exists(hero_dir):
os.mkdir(hero_dir)
# 进入创建好的文件夹
os.chdir(hero_dir)
i += 1
hero_count = 0
for k in range(max_num):
# 拼接url
onehero_link = downloadUrl + str(hero_number) + '/' + str(hero_number) + '-bigskin-' + str(k) + '.jpg'
im = requests.get(onehero_link) # 请求url
if im.status_code == 200:
picture_name = "_".join([hero_name, str(hero_number) , str(k)]) + '.jpg'
open(picture_name, 'wb').write(im.content) # 写入文件
print("[INFO]:下载完成:", os.path.join(hero_dir,picture_name))
hero_count += 1
print("[INFO]: 已获取", hero_name,"个数:",hero_count,"英雄图片完成...")
os.chdir(current_dir)
if __name__ == '__main__':
downloadPic()