前言
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import requests
# 请求天气的url地址
url= 'http://apis.juhe.cn/simpleWeather/query'
# 请求参数
data = {
"city":"上海",
"key":"331eab8f3481f37868378fcdc76cb7cd"
}
# 发送post请求
r = requests.post(url,data=data)
j = r.json()
print(j)
{
"reason":"查询成功!",
"result":{
"city":"上海",
"realtime":{
"temperature":"23",
"humidity":"25",
"info":"多云",
"wid":"01",
"direct":"西北风",
"power":"2级",
"aqi":"58"
},
"future":[
{
"date":"2023-03-15",
"temperature":"10/23℃",
"weather":"多云",
"wid":{
"day":"01",
"night":"01"
},
"direct":"西南风转东北风"
},
{
"date":"2023-03-16",
"temperature":"9/15℃",
"weather":"多云转小雨",
"wid":{
"day":"01",
"night":"07"
},
"direct":"东南风"
},
{
"date":"2023-03-17",
"temperature":"9/13℃",
"weather":"中雨转小雨",
"wid":{
"day":"08",
"night":"07"
},
"direct":"东南风转北风"
},
{
"date":"2023-03-18",
"temperature":"8/15℃",
"weather":"多云",
"wid":{
"day":"01",
"night":"01"
},
"direct":"北风转东南风"
},
{
"date":"2023-03-19",
"temperature":"12/16℃",
"weather":"晴",
"wid":{
"day":"00",
"night":"00"
},
"direct":"东南风"
}
]
},
"error_code":0
}
data = result['result']['future'][1]
print(data)
------------------------------输出结果---------------------------
{'date': '2023-03-16', 'temperature': '9/15℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东南风'}
data = jsonpath.jsonpath(result,'$..[?(@.date=="2023-03-16")]')
print(data)
-----------------------------输出结果----------------------------
[{'date': '2023-03-16', 'temperature': '9/15℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东南风'}]
# 直接获取到future的内容
data = jsonpath.jsonpath(result,'$.result.future')
data1 = jsonpath.jsonpath(result,'$.reason')
print(data)
print(data1)
-----------------------------输出结果----------------------
[[{'date': '2023-03-15', 'temperature': '10/23℃', 'weather': '多云', 'wid': {'day': '01', 'night': '01'}, 'direct': '西南风转东北风'}, {'date': '2023-03-16', 'temperature': '9/15℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东南风'}, {'date': '2023-03-17', 'temperature': '9/13℃', 'weather': '中雨转小雨', 'wid': {'day': '08', 'night': '07'}, 'direct': '东南风转北风'}, {'date': '2023-03-18', 'temperature': '8/15℃', 'weather': '多云', 'wid': {'day': '01', 'night': '01'}, 'direct': '北风转东南风'}, {'date': '2023-03-19', 'temperature': '12/16℃', 'weather': '晴', 'wid': {'day': '00', 'night': '00'}, 'direct': '东南风'}]]
['查询成功!']
data = jsonpath.jsonpath(result, '$.result.future.[*].date')
print(data)
-----------------------------输出结果----------------------
['2023-03-15', '2023-03-16', '2023-03-17', '2023-03-18', '2023-03-19']
data = jsonpath.jsonpath(result, '$..future[0,1]')
print(data)
-----------------------------输出结果----------------------
[{'date': '2023-03-15', 'temperature': '10/23℃', 'weather': '多云', 'wid': {'day': '01', 'night': '01'}, 'direct': '西南风转东北风'}, {'date': '2023-03-16', 'temperature': '9/15℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东南风'}]
http://www.atoolbox.net/Tool.php?Id=792