美文网首页
Python例子之下载王者荣耀全部英雄海报

Python例子之下载王者荣耀全部英雄海报

作者: By_syk | 来源:发表于2018-01-13 18:15 被阅读427次
    王昭君 - 精灵公主 海报

    海报来源:王者荣耀官网

    # 下载王者荣耀全部英雄海报
    # 海报尺寸为 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
            # 样例:http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/152/152-bigskin-2.jpg
            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

    def download_all():
        # ...
        data = res.read().decode('utf-8')
        
        # 移除 UTF-8 BOM 字符,避免 json.loads() 出错
        if data.startswith('\ufeff'):
            data = data.encode('utf-8')[3:].decode('utf-8')
    
        data_json = json.loads(data)
        # ...
    

    方案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()
    
    

    改进 #1

    # file_path = '%s/%s-%s.jpg' % (FOLDER_SAVE, hero_name, skin_name)  # 生成图片文件名
    # if os.path.exists(file_path):  # 避免重复下载
    #     print('[exists]')
    #     return
    file_path = '%s/%s-%s.jpg' % (FOLDER_SAVE, hero_name, skin_name)  # 生成图片文件名
    file_path_alt = '%s/%s-%s.jpg' % (FOLDER_SAVE, skin_name, hero_name)  # 用于除重
    if os.path.exists(file_path) | os.path.exists(file_path_alt):  # 避免重复下载
        print('[exists]')
        return
    

    相关文章

      网友评论

          本文标题:Python例子之下载王者荣耀全部英雄海报

          本文链接:https://www.haomeiwen.com/subject/ugzcoxtx.html