洋葱哥哥 发表于 2023-5-5 16:54

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

本帖最后由 洋葱哥哥 于 2023-5-5 17:08 编辑

之前上传过易语言编写的,接口已经失效了,最近在学Python正好拿来练习
调用了别人的接口,请勿攻击他人接口,仅供学习
用到了json和正则 没什么技术含量,供大家学习参考

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
    print("英雄名称:" + hero_name)
    platform = heroMsg
    print("查询大区:" + platform)
    area = heroMsg
    print("县级名称:" + area)
    area_power = heroMsg
    print("县级战力:" + area_power)
    city = heroMsg
    print("市级名称:" + city)
    city_power = heroMsg
    print("市级战力:" + city_power)
    province = heroMsg
    print("省级名称:" + province)
    province_power = heroMsg
    print("省级战力:" + province_power)
    guobiao = heroMsg
    print("国标战力:" + guobiao)

print("参数一传入英雄名字")
print("参数二传入查询大区,例如 aqq、awx、iqq、iwx")
print("-"*50)
search("戈娅", "awx")

paul2569 发表于 2023-5-5 19:49

看起来还可以

Naraci 发表于 2023-5-5 22:41

使用的第三方接口吧?

今夕何兮 发表于 2023-5-6 09:13

谢谢楼主分享

echoaku 发表于 2023-5-6 09:27

不错不错,感谢分享

xiaoliang0011 发表于 2023-5-15 09:30

不错不错,学习

zuoxuan2517 发表于 2023-6-5 08:51


不错不错,学习

w92vv 发表于 2023-7-8 12:22

我说最近访问怎么总卡了呢{:1_918:}
页: [1]
查看完整版本: 王者查询英雄战力,刚学py练习