python利用百度天气api查询天气数据

一.说明

本次实验的是百度天气的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
复制代码

四.参考资料

  1. 知乎-为什么我已经知道了python的基本语法,可还是不会写个类似天气预报或者能聊天的小软件?
  2. python结合API实现即时天气信息
  3. python 调用 API 获得的JSON如何处理才能获得我想获得的内容呢?
  4. 网上的天气 API 哪一个更加可靠?
  5. python day 08获取天气信息.制作天气预报软件
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值