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

这篇博客介绍了如何使用Python的requests、urllib.request和http.client库结合百度天气API查询并解析广州的天气数据,包括日期、城市、pm2.5值、未来几天的天气、风力和温度。同时,还展示了自定义JSON解析函数的方法。
摘要由CSDN通过智能技术生成

一.说明

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

四.参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值