示例代码
#-*- coding: UTF-8 -*-
'''
#################################################
# Author : 余欢
# Date : Dec 26, 2015 9:00:14 PM
#company : 南京师范大学--大数据实验室
# description : 读取JSON数据
#################################################
'''
import requests
import json
'''
1 指定GitHub URL(GitHub网站的最近活动时间表)来读取JSON格式数据
2 使用requests模块访问指定的URL,并获取内容
3 读取内容并将之转化为JSON格式的对象
4 迭代访问JSON对象,对于其中的每一项,读取每个代码库的URL值
'''
#树中的URL失效了
url = "http://api.map.baidu.com/telematics/v3/weather?location=%E6%88%90%E9%83%BD&output=json&ak=rnm8udmHdWaHFWZTO2tuTiG8"
r = requests.get(url) #获取内容
s = json.loads(r._content) #内容转化为json对象
# print s
print "============================"
print s["results"][0]["index"][0]["des"] #打印某项数据
json数据
{
"error":0,
"status":"success",
"date":"2015-12-27",
"results":
[
{
"currentCity":"成都",
"pm25":"291",
"index":
[
{
"title":"穿衣",
"zs":"较冷",
"tipt":"穿衣指数",
"des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},
{
"title":"洗车",
"zs":"较适宜",
"tipt":"洗车指数",
"des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},
{
"title":"旅游",
"zs":"适宜",
"tipt":"旅游指数",
"des":"天气较好,温度适宜,是个好天气哦。这样的天气适宜旅游,您可以尽情地享受大自然的风光。"},
{
"title":"感冒",
"zs":"极易发",
"tipt":"感冒指数",
"des":"昼夜温差极大,且空气湿度较大,寒冷潮湿,极易发生感冒,请特别注意增减衣服保暖防寒。"},
{
"title":"运动",
"zs":"较不宜",
"tipt":"运动指数",
"des":"天气较好,但考虑天气寒冷,推荐您进行室内运动,若在户外运动请注意保暖并做好准备活动。"},
{
"title":"紫外线强度",
"zs":"中等",
"tipt":"紫外线强度指数",
"des":"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"}
],
"weather_data":
[
{
"date":"周日 12月27日 (实时:3℃)",
"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather":"晴转多云",
"wind":"南风微风",
"temperature":"12 ~ 1℃"},
{
"date":"周一",
"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather":"阴转多云",
"wind":"南风微风",
"temperature":"11 ~ 4℃"},
{
"date":"周二",
"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png",
"weather":"多云转小雨",
"wind":"南风微风",
"temperature":"12 ~ 5℃"},
{
"date":"周三",
"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather":"多云",
"wind":"微风",
"temperature":"12 ~ 3℃"}
]
}
]
}
结果:
============================
建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。