【问题标题】:Openweathemap API saying temperature is like 282Openweathermap API 说温度是 282
【发布时间】: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


【解决方案1】:

正如documentation 所述,默认温度单位为Kelvin。要获取celsius unit,请使用units=metric

为了让它正确,我建议使用params 参数来传递 URL args

APP_ID = "..."
url = 'http://api.openweathermap.org/data/2.5/weather'
weather = requests.get(url, params={'units': 'metric',
                                    'appid': APP_ID,
                                    'q': weathercity})

【讨论】:

    【解决方案2】:
    import requests
    import time
    import sys
    
    weathercity = input("What city are you in? ")
    weather = requests.get('http://api.openweathermap.org/data/2.5/weather?q='+weathercity+'&appid=886705b4c1182eb1c69f28eb8c520e20')
    url = ('http://api.openweathermap.org/data/2.5/weather?q='+weathercity+'&appid=886705b4c1182eb1c69f28eb8c520e20')
    
    
    
    
    def spinning_cursor():
        while True:
            for cursor in '|/-\\':
                yield cursor
    
    
    data = weather.json()
    
    temp = data['main']['temp']
    description = data['weather'][0]['description']
    weatherprint ="In {}, it is currently {}°C with {}."
    spinner = spinning_cursor()
    for _ in range(25):
        sys.stdout.write(next(spinner))
        sys.stdout.flush()
        time.sleep(0.1)
        sys.stdout.write('\b')
    
    convert = int(temp - 273.15)
    print(weatherprint.format(weathercity, convert, description))
    

    【讨论】:

    • 这是我们的解决方案
    • 你想加入我吗?我有一群编码员。我今年 12 岁
    • 不,谢谢我很可能加入你的小组,顺便说一句,你应该 13 岁才能使用这个网站......谢谢你的回答,API 中有一个选项可以更改它跨度>
    猜你喜欢
    • 2018-08-22
    • 2016-05-06
    • 2016-08-30
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多