데이터 공부를 기록하는 공간

[CRAWLING] OPENAPI - smp 본문

STUDY/CRAWLING

[CRAWLING] OPENAPI - smp

BOTTLE6 2022. 5. 27. 23:41

■ Library 

import pandas as pd
import matplotlib.pyplot as plt
import datetime
import requests
from bs4 import BeautifulSoup

sns.set_context("talk")
sns.set_style("white")

import platform
from matplotlib import font_manager, rc

path="c:\Windows\Fonts\malgun.ttf"
if platform.system() == 'Darwin':
    rc('font',family='AppleGothic')
elif platform.system() == 'Windows':
    font_name=font_manager.FontProperties(fname=path).get_name()
    rc('font',family="Malgun Gothic")
else:
    print('Unknown system... sorry')  
import matplotlib as mpl
mpl.rcParams['axes.unicode_minus'] = False

■ crawling

serviceKey = "XXXXXXXXXXX"
url = "https://openapi.kpx.or.kr/openapi/smp1hToday/getSmp1hToday?areaCd=1&serviceKey={}".format(serviceKey)
res = requests.get(url) # verify=False 는 SLL 오류 발생시 활용
print("response 상태: {}".format(res.status_code))
soup = BeautifulSoup(res.text, 'html.parser')
soup

■ dataframe

tradedays = soup.find_all('tradeday')
tradehours = soup.find_all('tradehour')
smps = soup.find_all('smp')
hour = []
smp = []
for i,j in zip(tradedays, tradehours):
    hour.append(datetime.datetime(int(i.text[:4]), int(i.text[4:6]), int(i.text[6:]), int(j.text), 0, 0 ))
for k in smps:
    smp.append(float(k.text))
df = pd.DataFrame({"hour":hour, "smp":smp})
df.head()

■ graph

idx_max = np.argmax(df.smp)
idx_min = np.argmin(df.smp)

fig, ax = plt.subplots(figsize=(12,6))
ax.scatter(df.hour, df.smp, c='#1ee3cf')
ax.plot(df.hour, df.smp, c='#1ee3cf')
ax.grid()
ax.set_xlabel("hour")
ax.set_ylabel("smp")

# max
c_max = '#001871'
ax.scatter(df.loc[idx_max,'hour'], df.loc[idx_max,'smp'], c=c_max)
ax.text(df.loc[idx_max,'hour']+datetime.timedelta(minutes=30), df.loc[idx_max,'smp'], df.loc[idx_max,'hour'].strftime("%H")+"h : "+str(df.loc[idx_max,'smp']), c=c_max)
# min
c_min = '#ffb549'
ax.scatter(df.loc[idx_min,'hour'], df.loc[idx_min,'smp'], ls='--', c=c_min)
ax.text(df.loc[idx_min,'hour']+datetime.timedelta(minutes=30), df.loc[idx_min,'smp'], df.loc[idx_min,'hour'].strftime("%H")+"h : "+str(df.loc[idx_min,'smp']), c=c_min)
ax.set_title("{} SMP , 평균 : {:.2f} , 분산 : {:.2f}".format(df.hour[0].strftime("%Y-%m-%d"), df.smp.mean(), df.smp.std()))

#xticks
step = 3
xticks = [df.loc[i, 'hour'] for i in np.arange(0, df.shape[0], step)]
xticklabels = [i.strftime("%H") for i in xticks]
ax.set_xticks(xticks)
ax.set_xticklabels(xticklabels)

#ax.set_xlim(xticks[0]. xticks[-1])

'STUDY > CRAWLING' 카테고리의 다른 글

[crawling] requests - 배출권시세  (0) 2022.06.06
[crawling] requests - krx 시세 가져오기  (0) 2022.06.06
[CRAWLING] beautifulsoup-kema  (0) 2022.05.28
Comments