利用Python开发王者荣耀,一路直奔上王者
王者荣耀 -很火的手游-简直老少通吃-令人发指-虽然操作简单-但为什么你还是会被虐, 其实 是有技巧的--本文利用Python带你研究王者荣耀各类英雄的出装小技巧,让你成为大神般的存在
环境:Python3+ Windows
IDE:随意
模块:
- from urllib.request import urlretrieve
- import requests
首先找到三个接口
第一个是英雄武器的接口
# 武器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"
第二个是英雄列表接口
# 英雄列表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"
第三个是英雄出装的接口
第三个接口有点特殊,需要前面的英雄id,也就是hero_id
# 英雄出装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)
下面就是愉快的代码之旅了~
先热热身,通过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)
运行后下载图片:
接下来是获取英雄的名字和ID,代码如下:
# 打印所有英雄的名字和ID def hero_list(url,header): print('*' * 100) print(' 欢迎使用《王者荣耀》出装小助手!') 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传递一个 ,这样print函数不会在字符串末尾添加一个换行符,而是添加一个 print("%s的ID为:%s"%(each_hero['name'],each_hero['hero_id']),end = ' ') if flag == 3: # 先不加end 在加end 看效果 print(' ',end='') flag = 0
运行效果如图所示:
接下来就是出装的最主要部分
根据用户输入的英雄ID,查询出英雄的出装,以及总价
代码如下:
# 获取并打印出装信息 # weapon_info 所有武器的字典 def hero_info(url,header,weapon_info): req = requests.get(url=url, headers=header).json() print(" 历史上的%s: %s"%(req['info']['name'],req['info']['history_intro'])) for each_equip_choice in req['info']['equip_choice']: # print(each_equip_choice) print(' %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=' ') if flag == 3: print(' ', 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,也就是所有武器的字典,这个时候就需要在定义一个函数来获取武器的字典
# 获取武器信息 def hero_weapon(url,header): req = requests.get(url=url, headers=header).json() weapon_info_list = req['list'] return weapon_info_list
另附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' }
最终的运行效果下图:
其实还可以继续延伸的,可以做成一个APP,然后查询出装的,写上好看的界面!感觉就是一款很不错的APP,emmmmm可以类似以前的LOL盒子
写在最后
前几天有私信我要Python的学习资料,我连夜整理了一些有深度的Python教程和参考资料,从入门到高级的都有,文件已经打包好了,正在学习Python的同学可以免费下载学习学习。文件下载方式:点击我的头像,关注后私信回复“资料”即可下载。首先把代码撸起来!首先把代码撸起来!首先把代码撸起来!重要的事说三遍,哈哈。“编程是门手艺活”。什么意思?得练啊。