python用json解析爬取天气预报数据
2018-01-25 21:59
639 查看
#天气预报 # url 统一资源定位符 #windows+r cmd 打开命令 输入pip install requests 回车 #api.map.baidu.com(百度服务器地址) import requests #引入python中内置的包 import json while 1: print('*************欢迎进入天气查询系统**************') city=input('请输入您要查询的城市名称(按0退出):') if city=='0': print('您已退出天气查询系统!') break else: url='http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'%city #使用requests发送请求,接受返回的结果 response=requests.get(url) # print(type(response.text)) #使用loads函数,将json字符串转换为python的字典或列表 rs_dict=json.loads(response.text) #取出error error_code=rs_dict['error'] #如果取出的error为0,表示数据正常,否则没有查询到天气信息 if error_code==0: #从字典中取出数据 results=rs_dict['results'] #根据索引取出城市天气信息字典 info_dict=results[0] #根据字典的key 取出城市名称 city_name=info_dict['currentCity'] pm25=info_dict['pm25'] print('当前城市:%s pm值:%s'%(city_name,pm25)) #取出天气信息列表 weather_data=info_dict['weather_data'] #for循环取出每一天天气的小字典 for weather_dict in weather_data: #取出日期、天气、风级、温度 date=weather_dict['date'] weather=weather_dict['weather'] wind=weather_dict['wind'] temperature=weather_dict['temperature'] print('%s %s %s %s'%(date,weather,wind,temperature)) else: print('没有查询到天气信息!')
相关文章推荐
- <WP7>(三)手把手教你写天气预报程序:运用WebClient获取天气和json数据解析
- python解析json格式的天气数据
- Android阶段学习笔记 7.25-7.29 之 解析聚合数据 全国天气预报 Json
- Gson解析第三方提供Json数据(天气预报,新闻等)
- Python解析Yahoo的XML格式的天气预报数据
- Qt实现简易天气预报(解析的是json数据)
- <WP7>(三)手把手教你写天气预报程序:运用WebClient获取天气和json数据解析
- 【Android应用开发】-(17)获取Google天气预报通过Xml和JSON解析数据
- android JSON解析数据 android解析天气预报
- Gson解析第三方提供Json数据(天气预报,新闻等)
- Java如何像Python一样简洁地构造和解析Json 数据
- Android导入外部数据库解析json获取天气预报
- newtonsoft.json解析天气数据出错解决方法
- 基于python的天气预报(json)
- Java 和 Python 解析动态 key 的 JSON 数据
- 百度返回的JSON数据解析返回的objURL(python版本)
- python2.4 解析json数据
- 使用Python解析JSON数据的基本方法
- Python3 JSON 数据解析
- python解析json数据