歡迎光臨
每天分享高質量文章

大牛程式員利用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,essay-header):
   # 獲取文字.text 獲取圖片 .content
   req = requests.get(url = url,essay-headers = essay-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,essay-header):
   print('*' * 100)
   print('\t\t\t\t歡迎使用《王者榮耀》出裝小助手!')
   print('*' * 100)
   req = requests.get(url = url,essay-headers = essay-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

執行效果如圖所示:

接下來就是出裝的最主要部分

根據使用者輸入的英雄ID,查詢出英雄的出裝,以及總價

程式碼如下:

# 獲取並打印出裝資訊
# weapon_info  所有武器的字典
def hero_info(url,essay-header,weapon_info):
   req = requests.get(url=url, essay-headers=essay-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,也就是所有武器的字典,這個時候就需要在定義一個函式來獲取武器的字典

# 獲取武器資訊
def hero_weapon(url,essay-header):
   req = requests.get(url=url, essay-headers=essay-header).json()
   weapon_info_list = req['list']
   return weapon_info_list

另附essay-headers

essay-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盒子

贊(0)

分享創造快樂