一.说明
本次实验的是百度天气的api,网址为:click here
查询广州的天气,返回json格式,然后解析内容(上面链接点开就知道了)。(可以使用chrome应用Postman来pretiffy json内容)
二.示例代码
import requests
import urllib.request
import http.client
import json
#1.requests内置json解析方法
url = 'http://api.map.baidu.com/telematics/v3/weather?location=%E5%B9%BF%E5%B7%9E&output=json&ak=KPGX6sBfBZvz8NlDN5mXDNBF&callback='
r = requests.get(url)
print("查询日期:"+r.json()['date'])
print("城市:"+r.json()['results'][0]['currentCity'])
print("pm2.5值:"+r.json()['results'][0]['pm25'])
print("\n")
for i in range(0,4):
print("日期:"+r.json()['results'][0]['weather_data'][i]['date'])
print("天气:"+r.json()['results'][0]['weather_data'][i]['weather'])
print("风力:"+r.json()['results'][0]['weather_data'][i]['wind'])
print("温度:"+r.json()['results'][0]['weather_data'][i]['temperature'])
print("---------------")
#2.urllib库 + json库的json.loads()
url = 'http://api.map.baidu.com/telematics/v3/weather?location=%E5%B9%BF%E5%B7%9E&output=json&ak=KPGX6sBfBZvz8NlDN5mXDNBF&callback='
request = urllib.request.urlopen(url).read().decode('utf8')) #注意必须要.decode('utf8'),不然会有错误:the JSON object must be str, not 'bytes'
s = json.loads(request)
print("查询日期:"+s['date'])
print("城市:"+s['results'][0]['currentCity'])
print("pm2.5值:"+s['results'][0]['pm25'])
print("\n")
for i in range(0,4):
print("日期:"+s['results'][0]['weather_data'][i]['date'])
print("天气:"+s['results'][0]['weather_data'][i]['weather'])
print("风力:"+s['results'][0]['weather_data'][i]['wind'])
print("温度:"+s['results'][0]['weather_data'][i]['temperature'])
print("---------------")
#3.http.Client库 + json库的json.loads()
url = 'http://api.map.baidu.com/telematics/v3/weather?location=%E5%B9%BF%E5%B7%9E&output=json&ak=KPGX6sBfBZvz8NlDN5mXDNBF&callback='
httpClient = http.client.HTTPConnection('api.map.baidu.com', 80, timeout=30)
httpClient.request('GET',
'/telematics/v3/weather?location=%E5%B9%BF%E5%B7%9E&output=json&ak=KPGX6sBfBZvz8NlDN5mXDNBF&callback=')
response = httpClient.getresponse()
s = json.loads(response.read().decode('utf8')) #注意必须要.decode('utf8'),不然会有错误:the JSON object must be str, not 'bytes'
print("查询日期:"+s['date'])
print("城市:"+s['results'][0]['currentCity'])
print("pm2.5值:"+s['results'][0]['pm25'])
print("\n")
for i in range(0,4):
print("日期:"+s['results'][0]['weather_data'][i]['date'])
print("天气:"+s['results'][0]['weather_data'][i]['weather'])
print("风力:"+s['results'][0]['weather_data'][i]['wind'])
print("温度:"+s['results'][0]['weather_data'][i]['temperature'])
print("---------------")
复制代码
三.自己定义json解析函数
待解析json片段:
{
"showapi_res_code": 0,
"showapi_res_error": "",
"showapi_res_body": {
"pagebean": {
"allNum": 5034,
"allPages": 252,
"contentlist": [
{
"code2img": "http://app1.showapi.com/weixin_info/pubNum/xxxxxx.jpg",
"id": "55cbfce16e36a9c5946e40b0",
"pubNum": "xxxx",
"tag": "",
"type1_id": "44",
"type1_name": "名人明星",
"type2_id": "73",
"type2_name": "时尚",
"userLogo": "http://app1.showapi.com/weixin_info/pubNum/xxxx.jpg",
"weiNum": "xxx66 "
},
{
"code2img": "http://app1.showapi.com/weixin_info/pubNum/xxxx.jpg",
"id": "55cbfcdf6e36a9c5946e40ae",
"pubNum": "阳西县蓝星半岛旅游度假村",
"tag": "添加微信号:xxxx22 ",
"type1_id": "47",
"type1_name": "生活购物",
"type2_id": "100",
"type2_name": "旅游",
"userLogo": "http://app1.showapi.com/weixin_info/pubNum/xxxx.jpg",
"weiNum": "xxxx22"
}
复制代码
函数:
def json_path(d, path, sep='.'):
pp = path.split(sep)
t = d
for p in pp:
if type(t) is dict:
t = t[p]
elif type(t) is list:
t = t[int(p)]
else:
t = None
return t
import json
d = json.loads(s)
print json_path(d, "showapi_res_body.pagebean.contentlist.1.pubNum") #阳西县蓝星半岛旅游度假村
print json_path(d, "showapi_res_code") # 0
复制代码