爬取王者荣耀官网英雄及皮肤高清图片(多线程下载)

# -*- coding: utf-8 -*-
"""
@File    :  Glory of Kings.py
@Time    :  2021/1/8 17:08
@Author  :  Mr.Fu
@Contact :  [email protected]
@Software:  PyCharm
"""
# 爬虫自动下载王者荣耀官网英雄及英雄皮肤高清图片(多线程下载)
from concurrent.futures.thread import ThreadPoolExecutor

import requests


# 下载英雄皮肤图片
def download_picture(picture_url, hero_name):
    resp = requests.get(picture_url)
    if resp.status_code == 200:
        filename = hero_name + picture_url[picture_url.rfind('-') + 1:]
        # 在.py同级目录下创建文件夹 images
        with open(f'images/{filename}', 'wb') as file:
            file.write(resp.content)
    else:
        print('ERROR')


with ThreadPoolExecutor(max_workers=10) as pool:
    # 王者荣耀所有英雄的json格式数据
    resp = requests.get(url='https://pvp.qq.com/web201605/js/herolist.json')
    data_dict_list = resp.json()
    for hero_dict in data_dict_list:
        print('英雄:', hero_dict['cname'])
        if 'skin_name' in hero_dict:
            skin_name = hero_dict['skin_name']
            print('皮肤名:', skin_name)
            # 输出图片地址
            skin_num = skin_name.count('|')
            for x in range(0, skin_num + 1):
                href = f'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{str(hero_dict["ename"])}/{str(hero_dict["ename"])}-bigskin-{str(x + 1)}.jpg'
                print('海报网址:', href)
                pool.submit(download_picture, href, hero_dict['cname'])
        else:
            print('官网JSON文件残缺')
        print('******')

你可能感兴趣的:(Python学习,IT,免费,python,爬虫,json)