你将学到以下技能:
向网络API发起请求,解析和处理服务器返回的json数据,可以迁移到各种各样的API中,如PM2.5查询,道路拥堵查询,自然灾害查询等。
python字典数据类型的常用操作
以下的代码运行在jupyter notebook的开发环境中,这是python数据分析、机器学习、人工智能开发最常用的开发界面,因为可以非常方便的撰写博客、插入图片和数学公式,并输出代码运行的中间结果,强烈建议你学习如何使用jupyter notebook。
import urllib.request
import gzip
city_name = '上海'
# 将城市的中文名字编码成utf-8字符
urllib.parse.quote(city_name)
# 将编码后的城市名拼接在原始链接的后面
url = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)
weather_data = urllib.request.urlopen(url).read()
# 访问url链接,获取字节串数据
weather_data
# 将字节串解码为unicode编码
weather_data = gzip.decompress(weather_data)
weather_data
# 将unicode编码解码为utf-8编码,显示中文
weather_data = weather_data.decode('utf-8')
weather_data
# 将字符串两端的引号去掉,变成python中的字典数据
weather_dict = eval(weather_data)
weather_dict
type(weather_dict)
weather_dict
weather_dict['data']['yesterday']['high']
print('您查询的城市:',weather_dict['data']['city'])
print('--------------------------')
print('今天的天气')
print('温度',weather_dict['data']['wendu'])
print('感冒指数',weather_dict['data']['ganmao'])
print('--------------------------')
print('昨天的天气')
print('昨天:',weather_dict['data']['yesterday']['date'])
print('天气:',weather_dict['data']['yesterday']['type'])
print('最高气温:',weather_dict['data']['yesterday']['high'])
print('最低气温:',weather_dict['data']['yesterday']['low'])
print('风向:',weather_dict['data']['yesterday']['fx'])
print('风力:',weather_dict['data']['yesterday']['fl'][-5:-3])
print('--------------------------')
weather_dict[‘data’][‘forecast’]是一个包含五个元素的列表,每一个元素都是一个字典。
weather_dict['data']['forecast']
for each in weather_dict['data']['forecast']:
print('日期',each['date'])
print('天气',each['type'])
print(each['high'])
print(each['low'])
print('风向',each['fengxiang'])
print('风力:',each['fengli'][-5:-3])
print('--------------------------')
# 导入工具库
import urllib.request
import gzip
## 第一步:生成查询天气的url链接
city_name = input('请输入要查询的城市名称:')
# 将城市的中文名字编码成utf-8字符
urllib.parse.quote(city_name)
# 生成完整url链接
url = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
## 第二步:访问url链接,解析服务器返回的json数据,变成python的字典数据
# 获取服务器返回的json字节串数据
weather_data = urllib.request.urlopen(url).read()
# 将字节串数据解码为unicode中的utf-8数据
weather_data = gzip.decompress(weather_data).decode('utf-8')
# 将json数据转为python的字典数据
weather_dict = eval(weather_data)
if weather_dict.get('desc') == 'invilad-citykey':
print('您输入的城市未收录')
# 第三步:对字典进行索引,获取气温、风速、风向等天气信息
print('您查询的城市:',weather_dict['data']['city'])
print('--------------------------')
print('今天的天气')
print('温度',weather_dict['data']['wendu'])
print('感冒指数',weather_dict['data']['ganmao'])
print('--------------------------')
print('昨天的天气')
print('昨天:',weather_dict['data']['yesterday']['date'])
print('天气:',weather_dict['data']['yesterday']['type'])
print('最高气温:',weather_dict['data']['yesterday']['high'])
print('最低气温:',weather_dict['data']['yesterday']['low'])
print('风向:',weather_dict['data']['yesterday']['fx'])
print('风力:',weather_dict['data']['yesterday']['fl'][-5:-3])
print('--------------------------')
# 第四步:遍历forecast列表中的五个元素,打印天气信息
for each in weather_dict['data']['forecast']:
print('日期',each['date'])
print('天气',each['type'])
print(each['high'])
print(each['low'])
print('风向',each['fengxiang'])
print('风力:',each['fengli'][-5:-3])
print('--------------------------')