Python爬虫下载王者荣耀全皮肤

python爬虫下载王者荣耀全皮肤

import requests
import os
from time import time

start = time()
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/'
                         '69.0.3497.100 Safari/537.36',
           'Referer': 'http://pvp.qq.com/'}
data = requests.get('http://pvp.qq.com/web201605/js/herolist.json', headers=headers)
for i in range(len(data.json())):
    # 读取json信息
    name_id = str(data.json()[i]['ename'])
    name = data.json()[i]['cname']
    skin_name = data.json()[i]['skin_name'].split('|')
    # print(id, name, skin_name)

    # 创建图片储存路径
    path = 'C:\\Users\\Administrator\\Desktop\\python\\name_skin\\' + name
    if not os.path.exists(path):
        os.makedirs(path)
    # 下载并写入图片
    for j in range(len(skin_name)):
        url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' + name_id + '/' + name_id + '-bigskin-' \
              + str(j + 1) + '.jpg'
        picture = requests.get(url).content
        new_path = path + '\\' + skin_name[j] + '.jpg'
        with open(new_path, 'wb') as f:
            f.write(picture)
            print('正在下载', name, skin_name[j])
end = time()
print('下载完毕,程序运行时长{:.2f}s'.format(end - start))

你可能感兴趣的:(爬虫)