Python~JSON错误→ json.decoder.JSONDecodeError:预期值:第1行第1列(字符0)

2024-09-30 16:21:51 发布

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

我正在开发我的discord机器人,找到了一个我想要使用的api(名为numapi),我在我的pc上制作了一个原型,它是↓↓↓↓ 而且效果很好

import requests
import json

def numapi(no):
    response = requests.get(f'http://numbersapi.com/{no}?json')
    #print(response.json())
    json_data = json.loads(response.text)
    num = json_data['text']
    return(num)

while True:
    inc = input('enter ')

但是当我在我的discord机器人中复制同样的东西时,它就不起作用了。代码和错误如下:-

code

import requests
import json

client = discord.Client()

def numapi(no):
    #http://numbersapi.com/#505/year
    response = requests.get(f'http://numbersapi.com/{no}?json')
    #print(response.json())
    json_data = json.loads(response.text)
    num = json_data['text']
    return(num)

@client.event
async def on_message(message):
if message.content.startswith('$fact'):
        number = message.content.split('$fact', 1)[1]
        fact = numapi(number)
        #await message.channel.send(number)
        await message.channel.send(fact)

client.run(os.getenv("TOKEN"))

error

Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 291, in on_message
    fact = numapi(number)
  File "main.py", line 89, in numapi
    json_data = json.loads(response.text)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

请帮我解决这个问题


Tags: notextinpyimportjsonmessagedata