在python中将输入附加到api调用的末尾

2024-09-30 10:32:59 发布

您现在位置:Python中文网/ 问答频道 /正文

def i(bot,update,args):
    coin=args
    infoCall =requests.get("https://api.coingecko.com/api/v3/coins/").json()
    coinId = infoCall ['categories']
    update.message.reply_text(coinId)

我想在api请求的末尾添加coins=args中声明的args,以便它检索用户请求的信息,但这是我得到的错误

coinId = infoCall ['categories']
KeyError: 'categories'

我猜是因为它没有正确格式化请求,所以api给出的是404,而不是请求的信息

def i(bot,update,args):
    coin=args
    infoCall =requests.get("https://api.coingecko.com/api/v3/coins/").json()
    infoCall = json.loads(infoCall)+str(coins) 
    coinId = infoCall['categories']
    update.message.reply_text(str (coinId))

添加这个之后,这是我得到的新错误

    Traceback (most recent call last):
  File "C:\Users\Matthew\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telegram\ext\dispatcher.py", line 279, in process_update
    handler.handle_update(update, self)
  File "C:\Users\Matthew\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telegram\ext\commandhandler.py", line 173, in handle_update
    return self.callback(dispatcher.bot, update, **optional_args)
  File "C:/Users/Matthew/Desktop/coding_crap/CryptoBotBetav2.py", line 78, in i
    infoCall = json.loads(infoCall)+str(coins)
  File "C:\Users\Matthew\AppData\Local\Programs\Python\Python37-32\lib\json\__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not list

Tags: inpyapijsonlineargsupdateusers
1条回答
网友
1楼 · 发布于 2024-09-30 10:32:59

基本上,您没有将args参数附加到api点,这就是您得到错误的原因。在发出请求之前,需要将'bitcoin'附加到api点,而不是在输出上。 一个典型的例子如下。我已经删除了更新和其他未使用的变量。你可以把它们放在你需要的地方。你知道吗

import requests
def i(args):
    coin=args
    infoCall =requests.get("https://api.coingecko.com/api/v3/coins/"+ args).json()
    coinId = infoCall ['categories']
    print(coinId)
    # update.message.reply_text(coinId)
i('bitcoin')

输出:

['Cryptocurrency']

相关问题 更多 >

    热门问题