首页 > 解决方案 > 从不和谐的 api 请求 json 列表的问题

问题描述

hi = input("pls insert ans: ")

url = "https://covid-19-data.p.rapidapi.com/country"

querystring = {"name":hi, "format":"json"}

headers = {
    'x-rapidapi-key': "key",
    'x-rapidapi-host': "covid-19-data.p.rapidapi.com"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)

示例输出:

[
   {
      "country":"China",
      "code":"CN",
      "confirmed":92762,
      "recovered":87264,
      "critical":22,
      "deaths":4636,
      "latitude":35.86166,
      "longitude":104.195397,
      "lastChange":"2021-07-28T05:00:19+02:00",
      "lastUpdate":"2021-02:00"
   }
]

如果我这样说,它可以工作,但是当我将整个事情实现为不和谐时,它会返回一个空白列表。

因此,每当我想更改国家/地区时,我都必须每次在代码中手动更改它。

这是我的代码:

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith("$"):

        await message.channel.send("Country name?")

        def check(msg):
            return msg.channel == message.channel

        msg = await client.wait_for("message", check=check, timeout=30)   

        url = "https://covid-19-data.p.rapidapi.com/country"

        querystring = {"name":msg,"format":"json"}

        headers = {
                'x-rapidapi-key': "key",
                'x-rapidapi-host': "covid-19-data.p.rapidapi.com"
                }

        response = requests.request("GET", url, headers=headers, params=querystring)
        
        await message.channel.send(response.text)

输出:

[]

当我尝试从用户那里获取数据时,我得到了这个不和谐的空白列表。

标签: apidiscord

解决方案


推荐阅读