在线解析json并绘图
- 根据经纬度坐标+token处理json请求(只能http解析)
- pandas处理接受来的数据,中间涉及字段转换处理
- matplotlib绘图,时间轴,网格线,suptitle
# *
# * @author Zhaoliang Ye 叶昭良(zl_ye@qny.chng.com.cn)
# * @version V0.1
# * @Title: testJSON163.py
# * @Description: (用一句话描述该文件做什么?)
# * @Time: 2022/3/17 21:09
## https://www.cnblogs.com/BlueSkyyj/p/7594533.html 获取https的坑
# *
import json
import requests
import time
import jsonpath
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'}
format = '%Y-%m-%d %H:%M:%S' ### 日期格式
request_https_headers = {
'authority': "data.nba.net",
'method': "GET",
'path': "/10s/prod/v3/today.json",
'scheme': "https",
'accept': "application/json, text/plain, */*",
'accept-encoding': "gzip, deflate, br",
'accept-language': "en,en-US",
'origin': "https://www.nba.com",
'sec-fetch-dest': "empty",
'sec-fetch-mode': "cors",
'sec-fetch-site': "cross-site",
'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"
}
def get_json(url):
try:
response = requests.get(url, headers=headers)
# response = requests.get(url, headers=request_https_headers)
#response = requests.get(url)
if response.status_code == 200:
json_text = response.json()
return json_text
except Exception:
print('此页有问题!')
return None
def stampToTime(stamp): # 时间转换
year = stamp[0:4]
month = stamp[4:6]
day = stamp[6:8]
hour= stamp[8:10]
minute=0
second=0
currentDateTime = datetime.datetime.strptime(f'{year}-{month}-{day} {hour}:{minute}:{second}',format) ###起始时间
return currentDateTime
def get_comments(url):
data = []
doc = get_json(url)
jobs=doc['result']['sea']
for job in jobs:
dic = {}
#从根节点开始,匹配content节点
dic['seaSwellDirection(deg)']=jsonpath.jsonpath(job,'$..seaSwellDirection')[0] # 海浪向
dic['seaSwellHeight(m)']= jsonpath.jsonpath(job,'$..seaSwellHeight')[0] # 海浪高
dic['seaWaveDirection(deg)']=jsonpath.jsonpath(job,'$..seaWaveDirection')[0] # 涌浪向
dic['seaWaveHeight(m)']=jsonpath.jsonpath(job,'$..seaWaveHeight')[0]#涌浪高
dic['wind100Direction(deg)']=jsonpath.jsonpath(job,'$..wind100Direction')[0] # 百米风向
dic['wind100Speed(m/s)']=jsonpath.jsonpath(job,'$..wind100Speed')[0] #百米风速
dic['visibility(%)']=jsonpath.jsonpath(job,'$..visibility')[0] #能见度
dic['temp(℃)']=jsonpath.jsonpath(job,'$..temp')[0] #温度
dic['prate(mm)']=jsonpath.jsonpath(job,'$..prate')[0] #降水
#dic['time']=jsonpath.jsonpath(job,'$..time')[0] #UTC时间
dic['time']=stampToTime(jsonpath.jsonpath(job,'$..time')[0]) #UTC时间
data.append(dic)
return pd.DataFrame(data)
#final_result = get_comments('http://data.nba.net/10s/prod/v3/today.json')
final_result = get_comments('http://api.caiyunapp.com/v1/sea?token=VfgcXoLW7utFit&lng=121.50&lat=32.72')
# final_result.plot(subplots=True,figsize=(10,12),layout=(5,2),x_compat=True)
plt.rcParams['font.sans-serif']=['SimHei']
fig=plt.figure(figsize=(20,10))
fig.suptitle("如东H3风电场实时气象图 东经121°30' 北纬32°45'")
x=pd.to_datetime(final_result['time'])
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%h')) #To turn off Pandas Datetime tick adjustment,
plt.gca().xaxis.set_major_locator(mdates.HourLocator())
def createFig(subplotI,ydata,mycolor,mymarker,ylabel): # 时间转换
ax1 = plt.subplot(5, 2, subplotI)
p1 = ax1.scatter(x, final_result[ydata], color=mycolor, s=25, marker=mymarker)
plt.legend([p1], [ydata])
plt.ylabel(ylabel)
plt.grid(color='grey', linestyle='--', linewidth=0.5)
## 1
createFig(1,'seaSwellDirection(deg)','r','o','海浪向(°)')
createFig(2,'seaSwellHeight(m)','g','*','海浪高(m)')
createFig(3,'seaWaveDirection(deg)','r','o','涌浪向(°)')
createFig(4,'seaWaveHeight(m)','g','*','涌浪高(m)')
createFig(5,'wind100Direction(deg)','r','o','百米风向(°)')
createFig(6,'wind100Speed(m/s)','g','*','百米风速(m/s)')
createFig(7,'visibility(%)','r','o','能见度(%)')
createFig(8,'temp(℃)','g','*','温度(℃)')
createFig(9,'prate(mm)','b','o','降水量(mm)')
## 2
# ax2=plt.subplot(5,2,2)
# p2=ax2.scatter(x,final_result['seaSwellHeight(m)'],color='g',s=25,marker=".")
# plt.legend([p2],['seaSwellHeight(m)'])
# plt.ylabel('海浪高(m)')
# #final_result.plot()
# plt.grid(color='grey',linestyle='--',linewidth=0.5)
plt.show()
print(final_result)
print(final_result.head())
print(final_result['seaSwellHeight(m)'])
Related