python 爬虫提取王者荣耀英雄皮肤

仅供学习参考

 

import os
import requests

url = 'https://pvp.qq.com/web201605/js/herolist.json'
herolist = requests.get(url)  # 获取英雄列表json文件

herolist_json = herolist.json()  # 转化为json格式
hero_name = list(map(lambda x: x['cname'], herolist.json()))  # 提取英雄的名字
hero_number = list(map(lambda x: x['ename'], herolist.json()))  # 提取英雄的编号
hero_skin_number = []
for i in herolist.json():
    try:
        hero_skin_number.append(len(i['skin_name'].split("|")))
    except KeyError:
        hero_skin_number.append(1)

# 下载图片
def downloadPic():
    i = 0
    for j in hero_number:
        # 创建文件夹
        os.mkdir("D:\\spider\\" + hero_name[i])
        # 进入创建好的文件夹
        os.chdir("D:\\spider\\" + hero_name[i])
        i += 1
        for k in range(1, hero_skin_number[i-1]+1):
            # 拼接url
            onehero_link = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + str(j) + '/' + str(
                j) + '-bigskin-' + str(k) + '.jpg'
            im = requests.get(onehero_link)  # 请求url
            if im.status_code == 200:
                open(str(k) + '.jpg', 'wb').write(im.content)  # 写入文件


downloadPic()

 

你可能感兴趣的:(python)