feat: 10장 조회한 데이터 DB 저장
This commit is contained in:
parent
18016bfdec
commit
083153a367
@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
@ -6,6 +7,10 @@ import numpy as np
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import requests as rq
|
import requests as rq
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
import pymysql
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
GEN_OTP_URL = 'http://data.krx.co.kr/comm/fileDn/GenerateOTP/generate.cmd'
|
GEN_OTP_URL = 'http://data.krx.co.kr/comm/fileDn/GenerateOTP/generate.cmd'
|
||||||
DOWN_URL = 'http://data.krx.co.kr/comm/fileDn/download_csv/download.cmd'
|
DOWN_URL = 'http://data.krx.co.kr/comm/fileDn/download_csv/download.cmd'
|
||||||
@ -95,25 +100,49 @@ def get_total_stock_data(biz_day):
|
|||||||
# 데이터 정리
|
# 데이터 정리
|
||||||
# 종목, 개별 중 한군데만 있는 데이터 삭제(선박펀드, 광물펀드, 해외종목 등)
|
# 종목, 개별 중 한군데만 있는 데이터 삭제(선박펀드, 광물펀드, 해외종목 등)
|
||||||
diff = list(set(krx_sector['종목명']).symmetric_difference(set(krx_ind['종목명'])))
|
diff = list(set(krx_sector['종목명']).symmetric_difference(set(krx_ind['종목명'])))
|
||||||
kor_ticket = pd.merge(krx_sector, krx_ind, on=krx_sector.columns.intersection(krx_ind.columns).tolist(), how='outer')
|
kor_ticker = pd.merge(krx_sector, krx_ind, on=krx_sector.columns.intersection(krx_ind.columns).tolist(), how='outer')
|
||||||
# 일반적인 종목과 SPAC, 우선주, 리츠, 기타 주식을 구분
|
# 일반적인 종목과 SPAC, 우선주, 리츠, 기타 주식을 구분
|
||||||
kor_ticket['종목구분'] = np.where(kor_ticket['종목명'].str.contains('스팩|제[0-9]+호'), '스팩',
|
kor_ticker['종목구분'] = np.where(kor_ticker['종목명'].str.contains('스팩|제[0-9]+호'), '스팩',
|
||||||
np.where(kor_ticket['종목코드'].str[-1:] != '0', '우선주',
|
np.where(kor_ticker['종목코드'].str[-1:] != '0', '우선주',
|
||||||
np.where(kor_ticket['종목명'].str.endswith('리츠'), '리츠',
|
np.where(kor_ticker['종목명'].str.endswith('리츠'), '리츠',
|
||||||
np.where(kor_ticket['종목명'].isin(diff), '기타',
|
np.where(kor_ticker['종목명'].isin(diff), '기타',
|
||||||
'보통주'
|
'보통주'
|
||||||
))))
|
))))
|
||||||
kor_ticket = kor_ticket.reset_index(drop=True)
|
kor_ticker = kor_ticker.reset_index(drop=True)
|
||||||
kor_ticket.columns = kor_ticket.columns.str.replace(' ', '')
|
kor_ticker.columns = kor_ticker.columns.str.replace(' ', '')
|
||||||
kor_ticket = kor_ticket[['종목코드', '종목명', '시장구분', '종가',
|
kor_ticker = kor_ticker[['종목코드', '종목명', '시장구분', '종가',
|
||||||
'시가총액', '기준일', 'EPS', '선행EPS', 'BPS', '주당배당금', '종목구분']]
|
'시가총액', '기준일', 'EPS', '선행EPS', 'BPS', '주당배당금', '종목구분']]
|
||||||
kor_ticket = kor_ticket.replace({np.nan: None})
|
kor_ticker = kor_ticker.replace({np.nan: None})
|
||||||
kor_ticket['기준일'] = pd.to_datetime(kor_ticket['기준일'])
|
kor_ticker['기준일'] = pd.to_datetime(kor_ticker['기준일'])
|
||||||
return kor_ticket
|
return kor_ticker
|
||||||
|
|
||||||
|
|
||||||
|
def save_db(ticker):
|
||||||
|
con = pymysql.connect(user=os.getenv('DB_USER'),
|
||||||
|
passwd=os.getenv('DB_PW'),
|
||||||
|
host=os.getenv('DB_HOST'),
|
||||||
|
port=int(os.getenv('DB_PORT')),
|
||||||
|
db=os.getenv('DB_DB'),
|
||||||
|
charset='utf8')
|
||||||
|
|
||||||
|
mycursor = con.cursor()
|
||||||
|
query = f"""
|
||||||
|
insert into kor_ticker (종목코드,종목명,시장구분,종가,시가총액,기준일,EPS,선행EPS,BPS,주당배당금,종목구분)
|
||||||
|
values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) as new
|
||||||
|
on duplicate key update
|
||||||
|
종목명=new.종목명,시장구분=new.시장구분,종가=new.종가,시가총액=new.시가총액,EPS=new.EPS,선행EPS=new.선행EPS,
|
||||||
|
BPS=new.BPS,주당배당금=new.주당배당금,종목구분 = new.종목구분;
|
||||||
|
"""
|
||||||
|
|
||||||
|
args = ticker.values.tolist()
|
||||||
|
|
||||||
|
mycursor.executemany(query, args)
|
||||||
|
con.commit()
|
||||||
|
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
latest_biz_day = get_latest_biz_day()
|
latest_biz_day = get_latest_biz_day()
|
||||||
data = get_total_stock_data(latest_biz_day)
|
data = get_total_stock_data(latest_biz_day)
|
||||||
print(data)
|
save_db(data)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user