【发布时间】:2020-11-14 04:58:19
【问题描述】:
我一直在尝试在这里寻找解决此问题的方法,但似乎找不到重复的问题。我有一个天气命令,用户可以按邮政编码或城市搜索。打印 JSON 数据时,城市 URL 工作正常,但是当我输入 location 的邮政编码时,它给了我一个 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 错误。我打印出响应代码,它显示为 204 而不是 200 但是当我删除 if location == int(): 语句和另一个 URL 时,打印邮政编码 JSON 数据没有任何问题。
编辑:将我的代码从 if location == int() 更改为 if location.isdigit() 解决了这个问题。
if message.content.lower().startswith('!weather'):
x = message.content.split(" ", 1)
location = x[1]
if location == int():
url = 'https://api.weatherbit.io/v2.0/forecast/daily?postal_code={}&days=1&units=I&key=c85e55e66bfa4cb0ad9c21a3a038d59e'.format(location)
else:
url = 'https://api.weatherbit.io/v2.0/forecast/daily?city={}&units=I&key=c85e55e66bfa4cb0ad9c21a3a038d59e'.format(location)
res = requests.get(url)
information = res.json()
print(information)
【问题讨论】:
-
你不应该改变你的问题来实现答案。问题/答案将变得无关紧要,对其他人没有帮助。您应该只编辑问题以提供额外信息或解决问题(如果有)。 stackoverflow.com/help/privileges/edit
-
谢谢,我将其编辑回原始代码,但添加了一个编辑免责声明来解释我所做的更改。
标签: python-3.x discord.py discord.py-rewrite