Python例子之下載王者榮耀全部英雄海報

By_syk發表於2018-02-04

王昭君 - 精靈公主 海報

海報來源:王者榮耀官網

# 下載王者榮耀全部英雄海報
# 海報尺寸為 1920x882,截至目前共計 237 張,大小約 60 MB
# author: By_syk <By_syk@163.com>
# date: 2018-01-13


from urllib import request
import json
import os


# 英雄資料URL
URL_HERO = r'https://pvp.qq.com/web201605/js/herolist.json'
# 英雄海報圖片URL
URL_HERO_COVER_IMG = r'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/%d/%d-bigskin-%d.jpg'
# 圖片存放路徑
FOLDER_SAVE = r'E:/Download/QQPVPHeroCover'


def download_cover(hero_id, hero_name, cover_names):
    if not os.path.exists(FOLDER_SAVE):
        os.makedirs(FOLDER_SAVE)

    for i, cover_name in enumerate(cover_names):
        print('downloading:', hero_name, '-', cover_name)
        file_path = '%s/%s-%s.jpg' % (FOLDER_SAVE, hero_name, cover_name)  # 生成圖片檔名
        if os.path.exists(file_path):  # 避免重複下載
            continue
        # 樣例:https://user-gold-cdn.xitu.io/2018/2/4/1615ecdae82fce50?w=1920&h=882&f=jpeg&s=283248
        url_img = URL_HERO_COVER_IMG % (hero_id, hero_id, i + 1)  # 生成海報圖片URL
        res = request.urlopen(url_img)
        data = res.read()
        with open(file_path, 'wb') as file:
            file.write(data)


def download_all():
    print('start to download')
    res = request.urlopen(URL_HERO)
    data = res.read().decode('utf-8')
    data_json = json.loads(data)
    for hero_data in data_json:
        # 樣例:{'ename': 105, 'cname': '廉頗', 'title': '正義爆轟', 'new_type': 0, 'hero_type': 3, 'skin_name': '正義爆轟|地獄巖魂'}
        download_cover(int(hero_data['ename']),
                       hero_data['cname'],
                       hero_data['skin_name'].split('|'))
    print('all done')


if __name__ == '__main__':
    download_all()

複製程式碼

完整程式碼:Github / get_qq_moba_hero_cover

改進 #1

# res = request.urlopen(url_img)
#    data = res.read()
#    with open(file_path, 'wb') as file:
#        file.write(data)
request.urlretrieve(url_img, file_path)
複製程式碼

方案2

白起 - 最終兵器 海報

# 下載王者榮耀全部英雄海報
# 海報尺寸可選,截至目前共計 242 張,1080P 大小約 160 MB
# author: By_syk <By_syk@163.com>
# date: 2018-01-25


from urllib import request
import json
import os
import urllib

# 英雄資料URL
# 核心引數:頁次 page,每頁數量 iListNum
URL_HERO = r'http://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?' \
           r'sDataType=JSON&iActId=2735&iListNum=64&page=%d'
# 圖片存放路徑
FOLDER_SAVE = r'E:/Download/QQPVPHeroCover/1920x1080'


def download_cover(url_img, hero_and_skin_name):
    if not os.path.exists(FOLDER_SAVE):
        os.makedirs(FOLDER_SAVE)

    name_arr = hero_and_skin_name.split('-')
    hero_name = name_arr[0]
    skin_name = name_arr[1]
    print('downloading:', hero_name, '-', skin_name, end=' ')
    file_path = '%s/%s-%s.jpg' % (FOLDER_SAVE, hero_name, skin_name)  # 生成圖片檔名
    if os.path.exists(file_path):  # 避免重複下載
        print('[exists]')
        return
    # 樣例:http://shp.qpic.cn/ishow/2735092819/1475060911_1644740874_2501_sProdImgNo_6.jpg/0
    if url_img.endswith('/200'):  # 生成海報圖片 URL
        url_img = url_img[:-3] + '0'
    request.urlretrieve(url_img, file_path)
    print('[ok]')


def download_all():
    print('start to download')
    for i in range(0, 10):
        res = request.urlopen(URL_HERO % i)
        data = res.read().decode('utf-8')
        data_json = json.loads(data)
        # data['iTotalLines'] 總數
        core_json = data_json['List']
        if len(core_json) == 0:
            break
        for hero_data in core_json:
            # 桌布尺寸引數:
            # sProdImgNo_2: 1024x768
            # sProdImgNo_3: 1280x720
            # sProdImgNo_4: 1280x1024
            # sProdImgNo_5: 1440x900
            # sProdImgNo_6: 1920x1080
            # sProdImgNo_7: 1920x1200
            # sProdImgNo_8: 1920x1440
            download_cover(urllib.request.unquote(hero_data['sProdImgNo_6']),
                           urllib.request.unquote(hero_data['sProdName']))
    print('all done')


if __name__ == '__main__':
    download_all()

複製程式碼

相關文章