Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 코딩테스트
- GridSearchCV
- 실기
- 토익스피킹
- 파이썬
- 프로그래머스
- 비트코인
- backtest
- SQL
- 데이터분석
- 변동성돌파전략
- 데이터분석전문가
- 백테스트
- 파트5
- 주식
- lstm
- Crawling
- 빅데이터분석기사
- ADP
- 볼린저밴드
- hackerrank
- Python
- Quant
- TimeSeries
- sarima
- docker
- PolynomialFeatures
- Programmers
- 파이썬 주식
- randomforest
Archives
- Today
- Total
데이터 공부를 기록하는 공간
[CRAWLING] OPENAPI - smp 본문
■ 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