python全球天气预报

import urllib.request as r
#导入联网工具包,命名为r
url= ‘http://api.openweathermap.org/data/2.5/weather?q=hebi&mode=json&units=metric&lang=zh_cn&APPID=6a67ed641c0fda8b69715c43518b6996
data =r.urlopen(url).read().decode(‘utf-8’)
#将str类型转换为dict
import json
data =json.loads(data)

print(‘鹤壁今日温度’,data[‘main’][‘temp’],
‘天气情况’,data[‘weather’][0][‘description’],
‘天气气压’,data[‘main’][‘pressure’])

鹤壁今日温度 8.8 天气情况 多云 天气气压 1020.54
在这里插入图片描述

a=input(‘请输入城市,世界任何城市’)
import urllib.request as r
url=‘http://api.openweathermap.org/data/2.5/forecast?q={},cn&mode=json&lang=zh_cn&&APPID=6a67ed641c0fda8b69715c43518b6996&units=metric’.format(a)
rst=r.urlopen(url).read().decode(‘utf-8’)
import json
weather=json.loads(rst)

print(‘天气情况’,weather[‘list’][0][‘weather’][0][‘description’],’\n’, ‘气压’,weather[‘list’][0][‘main’][‘pressure’],’\n’, ‘最高温度’,weather[‘list’][0][‘main’][‘temp_max’],’\n’, ‘最低温度’,weather[‘list’][0][‘main’][‘temp_min’],’\n’, ‘当前温度’,weather[‘list’][0][‘main’][‘temp’],’\n’, ‘风’,weather[‘list’][0][‘wind’][‘speed’],’\n’, ‘发布时间’,weather[‘list’][0][‘dt_txt’],’\n’) input(’’)