接下来我们获取英雄和武器信息,详细代码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 HeroImagesPath="HeroiImages" HeroList=req['list'] for Hero in HeroList: HeroImageUrl=Hero['cover'] HeroName=Hero['name']+".jpg" FileName=HeroImagesPath+"/"+HeroName print "正在下载%s的图片"%Hero['name'] urllib.urlretrieve(url=HeroImageUrl, filename=FileName) """ 获取所有英雄的名字和ID """ def HeroList(url): print '-'*100 print ' 欢迎使用《王者》出装小助手' 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"])," " # if flag==3: # print " ","" # 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 " 历史上的%s: %s"%(req['info']['name'],req['info']['history_intro']) for eachEquipChoice in req['info']['equip_choice']: msg+=' '+(eachEquipChoice['title'])+':'+eachEquipChoice['description'] print " %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)+' ') print "%s:%s"%(weaponName,weaponPrice)," " if flag==3: print " ","" 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地址 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" # DownloadHeroImage(heros_url) # 武器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" weaponInfo=heroWeapon(weapon_url) # HeroList=HeroList(heros_url) for Hero in HeroList: print "%s的ID为:%s"%(Hero["name"],Hero["hero_id"])," " hero_id=Hero["hero_id"] 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) 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 * class Application(Frame): 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地址 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" weaponInfo = heroWeapon(weapon_url) # 英雄列表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" 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_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) msg=HeroInfo(hero_url, weaponInfo) tkMessageBox.showinfo('英雄信息', '%s'%msg) else: tkMessageBox.showinfo('英雄信息', '%s' % "此英雄不存在!!!") if __name__ == '__main__': app = Application() # 设置窗口标题: app.master.title('王者助手') # 主消息循环: app.mainloop()
执行结果如下:
还有其他图形界面的表示方法,大家可以自己挖掘一下。
有一个微信公众号“IT-LearnHall”,经常会分享一些python技术相关的干货,可以了解一下。