(博主亲自录制视频)
执行结果
详细参数
city #城市信息cod #内部参数cnt #预测天数list #信息列表,主要获取weatherData---weather---main和description信息message #内部参数
# -*- coding: utf-8 -*-#! python3# quickWeather.py - Prints the current weather for a location from the command line."""Created on Sat Apr 16 10:24:28 2016@author: daxiong"""import json, requests, sys'''# Compute location from command line arguments.if len(sys.argv) < 2: print('Usage: quickWeather.py location') sys.exit()location = ' '.join(sys.argv[1:])'''#城市名称cityName="Chongqing" #城市ID号cityID="1814905"#我的免费appIDappID="0383ae59663e14ab5a6855d5b6ad5d00"#获取天气天数days="3"# Download the JSON data from OpenWeatherMap.org's APIurl="http://api.openweathermap.org/data/2.5/forecast/city?id=%s&APPID=%s&cnt=%s"%(cityID,appID,days)response = requests.get(url)response.raise_for_status()# Load JSON data into a Python variable.weatherData = json.loads(response.text)# Print weather descriptions.#主要获取weatherData---weather---main和description信息w = weatherData['list']print('Current weather in %s:' % (cityName))print(w[0]['weather'][0]['main'], '-', w[0]['weather'][0]['description'])print()print('Tomorrow:')print(w[1]['weather'][0]['main'], '-', w[1]['weather'][0]['description'])print()print('Day after tomorrow:')print(w[2]['weather'][0]['main'], '-', w[2]['weather'][0]['description'])'''len(weatherData)Out[21]: 5for i in weatherData: print(i)city #城市信息cod #内部参数cnt #预测天数list #信息列表,主要获取weatherData---weather---main和description信息message #内部参数''''''weatherDataOut[23]: {'city': {'coord': {'lat': 30.25, 'lon': 107.75}, 'country': 'CN', 'id': 1814905, 'name': 'Chongqing Shi', 'population': 0, 'sys': {'population': 0}}, 'cnt': 1, 'cod': '200', 'list': [{'clouds': {'all': 44}, 'dt': 1460775600, 'dt_txt': '2016-04-16 03:00:00', 'main': {'grnd_level': 937.55, 'humidity': 98, 'pressure': 937.55, 'sea_level': 1025.81, 'temp': 288.16, 'temp_kf': 0.4, 'temp_max': 288.16, 'temp_min': 287.759}, 'rain': {'3h': 0.01}, 'sys': {'pod': 'd'}, 'weather': [{'description': 'light rain', 'icon': '10d', 'id': 500, 'main': 'Rain'}], 'wind': {'deg': 267.501, 'speed': 1.63}}], 'message': 0.0109}'''