如何使用python添加开放天气地图的天气地图1.0?

时间:2019-09-17 10:04:47

标签: python-3.x

如何使用python添加开放天气地图的天气地图1.0? https://openweathermap.org/api/weathermaps

1 个答案:

答案 0 :(得分:0)

import configparser
import requests
import sys

def get_api_key():
    config = configparser.ConfigParser()
    config.read('config.ini')
    return config['openweathermap']['api']

def get_weather(api_key, location):
    url = "https://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid= 
{}".format(location, api_key)

r = requests.get(url)
return r.json()

def main():
    if len(sys.argv) != 2:
        exit("Usage: {} LOCATION".format(sys.argv[0]))
    location = sys.argv[1]

    api_key = get_api_key()
    weather = get_weather(api_key, location)

    print(weather['main']['temp'])
    print(weather)


if __name__ == '__main__':
    main()