王者查询英雄战力,刚学py练习

查看 101|回复 6
作者:洋葱哥哥   
之前上传过易语言编写的,接口已经失效了,最近在学Python正好拿来练习
调用了别人的接口,请勿攻击他人接口,仅供学习
用到了json和正则 没什么技术含量,供大家学习参考
[Python] 纯文本查看 复制代码import requests
import re
import json
def search(heroName, checkPlatform):
    url = f"https://www.sapi.run/hero/select.php?hero={heroName}&type=" + checkPlatform
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers).text
    # print(response)
    # json 解析例子
    jsondata = json.loads(response)['data']
    # print(jsondata)
    print("这里是json解析例子")
    hero_name = jsondata['name']
    print("英雄名称:" + hero_name)
    platform = jsondata['platform']
    print("查询大区:" + platform)
    area = jsondata['area']
    print("县级名称:" + area)
    area_power = jsondata['areaPower']
    print("县级战力:" + area_power)
    city = jsondata['city']
    print("市级名称:" + city)
    city_power = jsondata['cityPower']
    print("市级战力:" + city_power)
    province = jsondata['province']
    print("省级名称:" + province)
    province_power = jsondata['provincePower']
    print("省级战力:" + province_power)
    guobiao = jsondata['guobiao']
    print("国标战力:" + guobiao)
    print("-"*50)
    # 正则获取例子
    heroMsg = re.findall('name":"(.*?)",.*?platform":"(.*?)",.*?area":"(.*?)","areaPower":"(.*?)","city":"(.*?)","cityPower":"(.*?)","province":"(.*?)","provincePower":"(.*?)","guobiao":"(.*?)",', response)
    # print(heroMsg)
    print("这里是正则表达式例子")
    hero_name = heroMsg[0][0]
    print("英雄名称:" + hero_name)
    platform = heroMsg[0][1]
    print("查询大区:" + platform)
    area = heroMsg[0][2]
    print("县级名称:" + area)
    area_power = heroMsg[0][3]
    print("县级战力:" + area_power)
    city = heroMsg[0][4]
    print("市级名称:" + city)
    city_power = heroMsg[0][5]
    print("市级战力:" + city_power)
    province = heroMsg[0][6]
    print("省级名称:" + province)
    province_power = heroMsg[0][7]
    print("省级战力:" + province_power)
    guobiao = heroMsg[0][8]
    print("国标战力:" + guobiao)
print("参数一传入英雄名字")
print("参数二传入查询大区,例如 aqq、awx、iqq、iwx")
print("-"*50)
search("戈娅", "awx")

战力, 名称

paul2569   

看起来还可以
wjr0221   

不错的功能
wertop   

谢谢分享。
Naraci   

使用的第三方接口吧?
今夕何兮   

谢谢楼主分享
echoaku   

不错不错,感谢分享
您需要登录后才可以回帖 登录 | 立即注册

返回顶部