api - 从不和谐的 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)
输出:
[]
当我尝试从用户那里获取数据时,我得到了这个不和谐的空白列表。
解决方案
推荐阅读
- c# - C# LINQ:'变量'x'的类型从范围''引用,但它没有定义'
- android - 如何保护我的应用程序文件在非根设备中被其他应用程序读/写?
- java - 流程图有问题吗?
- powerbi - 需要 Windows 授权时,如何使用 Power BI 发送自定义标头?
- flask - 通过连接授权的问题
- javascript - d3 - 单击时在 SVG 元素内添加粒子效果
- javascript - 将文件(图像、音频、视频)的确切存储大小从 Angularjs 保存到 WepApi
- django - django rest框架使用动作装饰器在url中添加查看财务报表的路径
- python - 如何仅使用后继函数在 python 中进行计算器?
- php - 有什么方法可以提高此代码的性能和执行时间?- PHP