如何使用python添加开放天气地图的天气地图1.0? https://openweathermap.org/api/weathermaps
答案 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()