python 爬取天气信息

url:http://api.openweathermap.org/data/2.5/forecast?q=wuhan&mode=json&units=metric&lang=zh_cn&APPID=6a67ed641c0fda8b69715c43518b6996

代码:

 

import urllib.request as req
import json

weather_url = "http://api.openweathermap.org/data/2.5/forecast?q=wuhan&mode=json&units=metric&lang=zh_cn&APPID=6a67ed641c0fda8b69715c43518b6996"

result = req.urlopen(weather_url).read().decode("utf-8")
print(result)
# 将字符串转换成json对象
result_json = json.loads(result)
print("="*30)

city = result_json['city']['name']
time = result_json['list'][0]['dt_txt']
weather = result_json['list'][0]['weather'][0]['description']
wind = result_json['list'][0]['wind']['speed']
temp = result_json['list'][0]['main']['temp']
pressure = result_json['list'][0]['main']['pressure']

len1 = len(result_json['list'])

for i in range(0,len1):
    print("城市名称:%s,天气:%s,气温:%s,气压:%s,时间:%s" %(result_json['city']['name'],result_json['list'][i]['weather'][0]['description'],str(result_json['list'][i]['main']['temp']),str(result_json['list'][i]['main']['pressure']),str(result_json['list'][i]['dt_txt'])))

 

运行截图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值