【发布时间】:2020-10-04 07:55:25
【问题描述】:
所以我用 python 编写了一个程序来告诉你天气,我正在使用 OpenWeatherMap API 来做到这一点。
预计当我得到信息时温度真的会下降吗?我认为这是一个 API,但我对此表示怀疑。
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"base":"stations","main":{"temp":282.48,"feels_like":277.86,"temp_min":282.04,"temp_max":283.15,"pressure":981,"humidity":87},"visibility":10000,"wind":{"speed":5.7,"deg":240},"rain":{"1h":1.15},"clouds":{"all":100},"dt":1601797507,"sys":{"type":1,"id":1414,"country":"GB","sunrise":1601791589,"sunset":1601832694},"timezone":3600,"id":2643743,"name":"London","cod":200}
我想要摄氏度的温度,但我不明白如何得到它?
这是我的代码:
weathercity = input("What city are you in? ")
weather = requests.get('http://api.openweathermap.org/data/2.5/weather?q='+weathercity+'&appid=...')
url = ('http://api.openweathermap.org/data/2.5/weather?q='+weathercity+'&appid=...')
data = weather.json()
temp = data['main']['temp']
description = data['weather'][0]['description']
weatherprint ="In {}, it is currently {}° with {}."
spinner = spinning_cursor()
for _ in range(25):
sys.stdout.write(next(spinner))
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\b')
print(weatherprint.format(weathercity, temp, description))
【问题讨论】:
-
没有看过文档,但几乎可以肯定是开尔文标度中的温度。减去 273.15 得到摄氏温度。这不是 python 问题
-
来自文档:“main.temp 温度。单位默认值:开尔文”。
-
是的,我知道这不是 python,但我不知道 api 从哪里得到数字/我不知道规模。也谢谢
-
也不要在公共空间分享您的私有 API 密钥(无论是哪个网站),它可能会冒充您
标签: python python-requests openweathermap