天气预报apipython_获取天气预报API

执行结果

详细参数

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}

'''

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值