大牛程序员利用Python开发王者荣耀带妹神器,一路直奔上王者
王者荣耀 -很火的手游-简直老少通吃-令人发指-虽然操作简单-但为什么你还是会被虐, 其实 是有技巧的–本文Python大神带你研究王者荣耀各类英雄的出装小技巧,让你成为大神般的存在
前期准备
环境:Python3+ Windows
IDE:随意
模块:
1 from urllib.request import urlretrieve 2 3 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"
# 英雄出装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下载王者荣耀得英雄图片,下面是代码部分:
1 # 下载王者荣耀英雄图片 2 def hero_imgs_download(url,header): 3 # 获取文本.text 获取图片 .content 4 req = requests.get(url = url,headers = header).json() 5 # 字典格式 6 # print((req)) 7 hero_num = len(req['list']) 8 print("一共有%d个英雄"%hero_num) 9 hero_images_path = 'hero_images' 10 hero_list = req['list'] 11 for each_hero in hero_list: 12 # print(each_hero) 13 hero_photo_url = each_hero['cover'] 14 hero_name = each_hero['name'] + '.jpg' 15 filename = hero_images_path + '/' + hero_name 16 print("正在下载 %s的图片"%each_hero['name']) 17 # if hero_images_path not in os.listdir(): 18 # os.makedirs(hero_images_path) 19 # 下载图片 20 urlretrieve(url = hero_photo_url,filename = filename)
运行后下载图片:
接下来是获取英雄的名字和ID,代码如下:
1 # 打印所有英雄的名字和ID 2 def hero_list(url,header): 3 print('*' * 100) 4 print('\t\t\t\t欢迎使用《王者荣耀》出装小助手!') 5 print('*' * 100) 6 req = requests.get(url = url,headers = header).json() 7 flag = 0 8 hero_list = req['list'] 9 for each_hero in hero_list: 10 flag += 1 11 # 为end传递一个\t,这样print函数不会在字符串末尾添加一个换行符,而是添加一个\t 12 print("%s的ID为:%s"%(each_hero['name'],each_hero['hero_id']),end = '\t\t') 13 if flag == 3: 14 # 先不加end 在加end 看效果 15 print('\n',end='') 16 flag = 0
运行效果如图所示:
1 # 获取并打印出装信息 2 #Python学习交流群:125240963 3 # weapon_info 所有武器的字典 4 def hero_info(url,header,weapon_info): 5 req = requests.get(url=url, headers=header).json() 6 print("\n历史上的%s:\n %s"%(req['info']['name'],req['info']['history_intro'])) 7 for each_equip_choice in req['info']['equip_choice']: 8 # print(each_equip_choice) 9 print('\n%s:%s'%(each_equip_choice['title'],each_equip_choice['description'])) 10 flag = 0 11 total_price = 0 12 for each_weapon in each_equip_choice['list']: 13 flag += 1 14 weapon = seek_weapon(each_weapon['equip_id'],weapon_info) 15 # print(weapon) 16 weapon_name = weapon[0] 17 weapon_price = weapon[1] 18 print('%s:%s' % (weapon_name, weapon_price), end='\t') 19 if flag == 3: 20 print('\n', end='') 21 flag = 0 22 total_price += int(weapon_price) 23 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
另附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' }
最终的运行效果下图:
源代码获取地址