Python實現王者榮耀小助手(二)

2021-08-20 20:19:20 字數 4644 閱讀 4416

接下來我們獲取英雄和**資訊,詳細**kingglory.py如下(**中有詳細註解):

# -*- coding: utf-8 -*-

#!/usr/bin/env python

# @time : 2018/6/15 17:12

# @desc :

# @file : kingglory.py

# @software: pycharm

"""導入庫

"""import urllib

import requests,sys

"""統一編碼

"""reload(sys)

sys.setdefaultencoding('utf-8')

"""獲取英雄

"""def downloadheroimage(url):

req=requests.get(url=url).json()

heronum=len(req['list'])

print "一共有%d個英雄"%heronum

"""獲取所有英雄的名字和id

"""def herolist(url):

print '-'*100

print '\t\t\t\t歡迎使用《王者》出裝小助手'

print '-'*100

req=requests.get(url=url).json()

flag=0

herolist=req['list']

# for hero in herolist:

# flag+=1

# print "%s的id為:%s"%(hero["name"],hero["hero_id"]),"\t"

# if flag==3:

# print "\n",""

# flag=0

return herolist

"""獲取並列印出裝資訊

weapon所有**的字典

"""msg=''

def heroinfo(url,weaponinfo):

req = requests.get(url=url).json()

# msg=''

msg='歷史上的%s:%s'%(req['info']['name'],req['info']['history_intro'])

# print "\n歷史上的%s:\n %s"%(req['info']['name'],req['info']['history_intro'])

for eachequipchoice in req['info']['equip_choice']:

msg+='\n'+(eachequipchoice['title'])+':'+eachequipchoice['description']

print "\n%s:%s"%(eachequipchoice["title"],eachequipchoice["description"])

flag=0

totalprice=0

for eachweapon in eachequipchoice['list']:

flag+=1

weapon=seekweapon(eachweapon['equip_id'],weaponinfo)

weaponname=weapon[0]

weaponprice=weapon[1]

msg+=('%s:%s'%(weaponname,weaponprice)+'\t')

print "%s:%s"%(weaponname,weaponprice),"\t"

if flag==3:

print "\n",""

flag=0

totalprice+=int(weaponprice)

msg+=u'神裝套件總計:'+str(totalprice)

# print "神裝套件總計:%d"%totalprice

return msg

"""根據equip_id查詢**名字和**

weaponinfo儲存所有**的字典

"""def seekweapon(equipid,weaponinfo):

for weapon in weaponinfo:

if weapon['equip_id']==str(equipid):

weaponname=weapon['name']

weaponprice=weapon['price']

return weaponname,weaponprice

"""獲取**資訊

"""def heroweapon(url):

req=requests.get(url=url).json()

weaponlist=req['list']

return weaponlist

if __name__ == '__main__':

# 英雄列表url位址

# downloadheroimage(heros_url)

# **url位址

weaponinfo=heroweapon(weapon_url)

#herolist=herolist(heros_url)

for hero in herolist:

print "%s的id為:%s"%(hero["name"],hero["hero_id"]),"\t"

hero_id=hero["hero_id"]

hero_id)

heroinfo(hero_url,weaponinfo)

執行結果如下:

建立圖形化介面views.py**如下(**中有詳細註解):

# -*- coding: utf-8 -*-

#!/usr/bin/env python

# @time : 2018/6/19 14:37

# @desc :

# @file : views.py

# @software: pycharm

from tkinter import *

import tkmessagebox

from kingglory import *

def __init__(self, master=none):

frame.__init__(self, master)

self.pack()

self.createwidgets()

def createwidgets(self):

self.nameinput = entry(self,width=40)

self.nameinput.pack()

self.alertbutton = button(self, text='請輸入英雄名', command=self.hello)

self.alertbutton.pack()

def hello(self):

# **url位址

weaponinfo = heroweapon(weapon_url)

# 英雄列表url位址

data = self.nameinput.get()

herolis = herolist(heros_url)

herodir={}

for hero in herolis:

herodir[hero['name']]=hero['hero_id']

if herodir.has_key(data):

hero_id = herodir[data]

hero_id)

msg=heroinfo(hero_url, weaponinfo)

tkmessagebox.showinfo('英雄資訊', '%s'%msg)

else:

tkmessagebox.showinfo('英雄資訊', '%s' % "此英雄不存在!!!")

if __name__ == '__main__':

# 主訊息迴圈:

執行結果如下:

還有其他圖形介面的表示方法,大家可以自己挖掘一下。

it-learnhall

」,經常會分享一些python技術相關的乾貨,可以了解一下。

python實現王者榮耀小助手(一)

Python實現王者榮耀小助手(一)

簡單來說網路爬蟲,是指抓取全球資訊網資訊的程式或者指令碼 python在 網路爬蟲有很大優勢,今天我們用python實現獲取王者榮耀相關資料,做乙個小助手 前期準備,環境搭建 python2.7 sys模組提供了許多函式和變數來處理 python 執行時環境的不同部分 urllib模組提供了一系列用...

實現用python刷王者榮耀金幣

刷冒險最後一關,需要滿符文 需要安裝adb,可以網上查閱怎麼安裝 將adb路徑加入環境變數 coding utf 8 created on wed feb 20 13 48 11 2019 qq區 趙雲,魯班,扁鵲 author 鄧磊 from subprocess import run impor...

學習札記之python 王者榮耀面板桌布小爬蟲

當初閒來無聊,一時興起學python的目的就是爬一下 的桌布,如今小目標已經達成了,下一步就是2d小遊戲了,加油!用了一點正規表示式的知識 import urllib.request as ureq import os import re from urllib import error def u...