美文网首页python热爱者
大牛程序员利用Python开发王者荣耀带妹神器,一路直奔上王者

大牛程序员利用Python开发王者荣耀带妹神器,一路直奔上王者

作者: 3c6af12b4629 | 来源:发表于2018-05-29 16:22 被阅读0次
    image

    王者荣耀 -很火的手游-简直老少通吃-令人发指-虽然操作简单-但为什么你还是会被虐, 其实 是有技巧的--本文Python大神带你研究王者荣耀各类英雄的出装小技巧,让你成为大神般的存在

    前期准备

    环境:Python3+ Windows

    IDE:随意

    模块:

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">1 from urllib.request import urlretrieve 2
    3 import requests</pre>

    首先找到三个接口

    第一个是英雄武器的接口

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"># 武器URL地址
    weapon_url = "http://gamehelper.gm825.com/wzry/equip/list?channel_id=90009a&app_id=h9044j&game_id=7622&game_name=%E7%8E%8B%E8%80%85%E8%8D%A3%E8%80%80&vcode=12.0.3&version_code=1203&cuid=2654CC14D2D3894DBF5808264AE2DAD7&ovr=6.0.1&device=Xiaomi_MI+5&net_type=1&client_id=1Yfyt44QSqu7PcVdDduBYQ%3D%3D&info_ms=fBzJ%2BCu4ZDAtl4CyHuZ%2FJQ%3D%3D&info_ma=XshbgIgi0V1HxXTqixI%2BKbgXtNtOP0%2Fn1WZtMWRWj5o%3D&mno=0&info_la=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&info_ci=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&mcc=0&clientversion=&bssid=VY%2BeiuZRJ%2FwaXmoLLVUrMODX1ZTf%2F2dzsWn2AOEM0I4%3D&os_level=23&os_id=dc451556fc0eeadb&resolution=1080_1920&dpi=480&client_ip=192.168.0.198&pdunid=a83d20d8"</pre>
    
    

    第二个是英雄列表接口

    
    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"># 英雄列表URL地址
    heros_url = "http://gamehelper.gm825.com/wzry/hero/list?channel_id=90009a&app_id=h9044j&game_id=7622&game_name=%E7%8E%8B%E8%80%85%E8%8D%A3%E8%80%80&vcode=12.0.3&version_code=1203&cuid=2654CC14D2D3894DBF5808264AE2DAD7&ovr=6.0.1&device=Xiaomi_MI+5&net_type=1&client_id=1Yfyt44QSqu7PcVdDduBYQ%3D%3D&info_ms=fBzJ%2BCu4ZDAtl4CyHuZ%2FJQ%3D%3D&info_ma=XshbgIgi0V1HxXTqixI%2BKbgXtNtOP0%2Fn1WZtMWRWj5o%3D&mno=0&info_la=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&info_ci=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&mcc=0&clientversion=&bssid=VY%2BeiuZRJ%2FwaXmoLLVUrMODX1ZTf%2F2dzsWn2AOEM0I4%3D&os_level=23&os_id=dc451556fc0eeadb&resolution=1080_1920&dpi=480&client_ip=192.168.0.198&pdunid=a83d20d8"</pre>
    
    

    第三个是英雄出装的接口

    第三个接口有点特殊,需要前面的英雄id,也就是hero_id

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: &quot;Courier New&quot; !important; font-size: 12px !important;"># 英雄出装URL
    hero_url = "http://gamehelper.gm825.com/wzry/hero/detail?hero_id={}&channel_id=90009a&app_id=h9044j&game_id=7622&game_name=%E7%8E%8B%E8%80%85%E8%8D%A3%E8%80%80&vcode=12.0.3&version_code=1203&cuid=2654CC14D2D3894DBF5808264AE2DAD7&ovr=6.0.1&device=Xiaomi_MI+5&net_type=1&client_id=1Yfyt44QSqu7PcVdDduBYQ%3D%3D&info_ms=fBzJ%2BCu4ZDAtl4CyHuZ%2FJQ%3D%3D&info_ma=XshbgIgi0V1HxXTqixI%2BKbgXtNtOP0%2Fn1WZtMWRWj5o%3D&mno=0&info_la=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&info_ci=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&mcc=0&clientversion=&bssid=VY%2BeiuZRJ%2FwaXmoLLVUrMODX1ZTf%2F2dzsWn2AOEM0I4%3D&os_level=23&os_id=dc451556fc0eeadb&resolution=1080_1920&dpi=480&client_ip=192.168.0.198&pdunid=a83d20d8".format(hero_id)</pre>
    
    

    下面就是愉快的代码之旅了~

    先热热身,通过urllib下载王者荣耀得英雄图片,下面是代码部分:

    # 下载王者荣耀英雄图片
    def hero_imgs_download(url,header):
        # 获取文本.text 获取图片 .content
        req = requests.get(url = url,headers = header).json()
        # 字典格式
        # print((req))
        hero_num = len(req['list'])
        print("一共有%d个英雄"%hero_num)
        hero_images_path = 'hero_images'
        hero_list = req['list']
        for each_hero in hero_list:
            # print(each_hero)
            hero_photo_url = each_hero['cover']
            hero_name = each_hero['name'] + '.jpg'
            filename = hero_images_path + '/' + hero_name
            print("正在下载 %s的图片"%each_hero['name'])
            # if hero_images_path not in os.listdir():
            #     os.makedirs(hero_images_path)
            # 下载图片
            urlretrieve(url = hero_photo_url,filename = filename)
    

    运行后下载图片:

    image

    接下来是获取英雄的名字和ID,代码如下:

    # 打印所有英雄的名字和ID
    def hero_list(url,header):
        print('*' * 100)
        print('\t\t\t\t欢迎使用《王者荣耀》出装小助手!')
        print('*' * 100)
        req = requests.get(url = url,headers = header).json()
        flag = 0
        hero_list = req['list']
        for each_hero in hero_list:
            flag += 1
            # 为end传递一个\t,这样print函数不会在字符串末尾添加一个换行符,而是添加一个\t
            print("%s的ID为:%s"%(each_hero['name'],each_hero['hero_id']),end = '\t\t')
            if flag == 3:
                # 先不加end  在加end 看效果
                print('\n',end='')
                flag = 0
    
    运行效果如图所示:
    image

    接下来就是出装的最主要部分

    根据用户输入的英雄ID,查询出英雄的出装,以及总价

    代码如下:

    # 获取并打印出装信息
    #Python学习交流群:125240963
    # weapon_info  所有武器的字典
    def hero_info(url,header,weapon_info):
        req = requests.get(url=url, headers=header).json()
        print("\n历史上的%s:\n %s"%(req['info']['name'],req['info']['history_intro']))
        for each_equip_choice in req['info']['equip_choice']:
            # print(each_equip_choice)
            print('\n%s:%s'%(each_equip_choice['title'],each_equip_choice['description']))
            flag = 0
            total_price = 0
            for each_weapon in each_equip_choice['list']:
                flag += 1
                weapon = seek_weapon(each_weapon['equip_id'],weapon_info)
                # print(weapon)
                weapon_name = weapon[0]
                weapon_price = weapon[1]
                print('%s:%s' % (weapon_name, weapon_price), end='\t')
                if flag == 3:
                    print('\n', end='')
                    flag = 0
                total_price += int(weapon_price)
            print("神装套件共计:%d"%total_price)
    

    这个时候运行会有一些问题,seek_weapon函数还没有定义,接下来定义seek_weapon,代码如下:

    # 根据equip_id查询武器名字和价格
    # weapon_info - 存储所有武器的字典
    def seek_weapon(equip_id,weapon_info):
        for each_weapon in weapon_info:
            if each_weapon['equip_id'] == str(equip_id):
                weapon_name = each_weapon['name']
                weapon_price = each_weapon['price']
                return weapon_name,weapon_price
    

    seek_weapon函数在调用的时候,需要weapon_info,也就是所有武器的字典,这个时候就需要在定义一个函数来获取武器的字典

    1 # 获取武器信息
    2 def hero_weapon(url,header): 
    3     req = requests.get(url=url, headers=header).json() 
    4     weapon_info_list = req['list'] 
    5     return weapon_info_list</pre>
    

    另附headers

    headers = {
        'Accept-Charset': 'UTF-8',
        'Accept-Encoding': 'gzip,deflate',
        'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 6.0.1; MI 5 MIUI/V8.1.6.0.MAACNDI)',
        'X-Requested-With': 'XMLHttpRequest',
        'Content-type': 'application/x-www-form-urlencoded',
        'Connection': 'Keep-Alive',
        'Host': 'gamehelper.gm825.com'
    }
    

    最终的运行效果下图:

    image

    相关文章

      网友评论

        本文标题:大牛程序员利用Python开发王者荣耀带妹神器,一路直奔上王者

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