获取天气预报API

sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频)

https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campaign=commission&utm_source=cp-400000000398149&utm_medium=share

执行结果

详细参数

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"
#我的免费appID
appID="0383ae59663e14ab5a6855d5b6ad5d00"
#获取天气天数
days="3"
# Download the JSON data from OpenWeatherMap.org's API
url="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]: 5
for i in weatherData:
    print(i)
city  #城市信息
cod   #内部参数
cnt   #预测天数
list  #信息列表,主要获取weatherData---weather---main和description信息
message #内部参数

'''


'''
weatherData
Out[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}

'''

  

原文地址:https://www.cnblogs.com/webRobot/p/5397892.html