Python语言基础--6(json解析)
2018-01-23 11:12
459 查看
#python 2.7 #coding:utf-8 #url 统一资源定位符 #在python中发送请求 #下载requests包,用其发送请求 #windows+r cmd 输入 pip install requests import requests import json #1.准备url地址 while 1: l=raw_input('请输入要查询城市:') url='http://api.map.baidu.com/telematics/v3/weather?location={}&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'.format(l) #url= 'http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'%l #2.发送一个get请求,获取url地址下的资源内容 #get(url) # response 接收服务器返回的响应数据 response=requests.get(url) #print response.content #将json中的字符串转换为python中的字典或列表 weather_dict=json.loads(response.content) #print type(weather_dict) #根据key取出字典中对应值 date=weather_dict.get('date') #print date #取出 results列表 results=weather_dict['results'] #取出results中字典 detail_dict=results[0] current_city=detail_dict['currentCity'] print '当前城市:',current_city pm25=int(detail_dict['pm25']) if 0<=pm25<=50: print 'PM2.5:',pm25,'优' elif pm25<=100: print 'PM2.5:', pm25, '良' elif pm25<=150: print 'PM2.5:', pm25, '轻度污染' elif pm25<=200: print 'PM2.5:', pm25, '中度污染' elif pm25<=300: print 'PM2.5:', pm25, '重度污染' else: print 'PM2.5:', pm25, '严重污染' #取出index列表 detail_index=detail_dict['index'] #取出index列表中第一个字典 for i in range(0,5): detail_index_dict=detail_index[i] #取出字典中相应内容 print '********************' print detail_index_dict['title'],detail_index_dict['zs'] print '********************' print detail_index_dict['tipt'],detail_index_dict['des'] detail_weather_data=detail_dict['weather_data'] for j in range(0,4): detail_weather_data_dict=detail_weather_data[j] print '********************' print detail_weather_data_dict['date'],detail_weather_data_dict['weather'],detail_weather_data_dict['wind'] print detail_weather_data_dict['temperature']
相关文章推荐
- Python语言基础--6.2(Json解析)
- Python语言基础--6.1(Json解析)
- python解析json基础
- Python语言基础之计时,pickle,列表解析
- Python3基础 解析json格式的数据,得到一个字典 json.loads()
- 【脚本语言系列】关于Python基础知识枚举,你需要知道的事
- 使用python解析Json字符串-获取Json字符串关键字
- python自动化--语言基础五面向对象、迭代器、range和切片的区分
- 坚持#第231天~零基础自学云计算基础语言应用之python第7节
- 零基础掌握百度地图兴趣点获取POI爬虫(python语言爬取)(代码篇)
- 使用Jquery解析Json基础知识(转)
- OC语言学习14-JSON数据解析
- 使用python解析json文件
- 基于python的-json解析爬取电影接口(存入html中)
- Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
- 一个脚本讲述python语言的基础规范,适合初学者
- Python序列化基础知识(json/pickle)
- CDays-3 习题二 (字典及文件读取练习)及相关内容解析。Python 基础教程
- 【脚本语言系列】关于Python基础知识映射器和过滤器,你需要知道的事
- Python解析JSON详解