-
Notifications
You must be signed in to change notification settings - Fork 0
/
kosdaq_data.py
55 lines (46 loc) · 1.95 KB
/
kosdaq_data.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from dotenv import load_dotenv
import FinanceDataReader as fdr
import pymysql
import pymysql.cursors
import os
import datetime
## Stock list
STOCK_MARKET = "KOSDAQ"
BATCH_SIZE = 100
## MySQL Connection Info
load_dotenv()
host = os.environ.get("DB_HOST")
database = os.environ.get("DB_NAME")
user = os.environ.get("DB_USER")
password = os.environ.get("DB_PASSWORD")
## MySQL 연결
connection = pymysql.connect(host=host, user=user, password=password, database=database, cursorclass=pymysql.cursors.DictCursor)
df_kosdaq = fdr.StockListing('KOSDAQ')
print(df_kosdaq.head())
try:
with connection.cursor() as cursor:
query = """INSERT INTO Stock (stock_code, stock_name, market_location, market_type, market_cap,updated_at)
VALUES(%s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
market_cap = VALUES(market_cap),
updated_at = VALUES(updated_at)"""
for start_row in range(0, len(df_kosdaq), BATCH_SIZE):
batch_data = df_kosdaq.iloc[start_row:start_row + BATCH_SIZE]
# 데이터 준비
data_to_insert = []
for idx, row in batch_data.iterrows():
stock_code = row['Code'] # FinanceDataReader의 'Code' 대신 'Symbol'을 사용할 수 있습니다
stock_name = row['Name']
market_cap = row['Marcap'] # 시가 총액
market_location = 0 # 국내주식
market_type = STOCK_MARKET
updated_at = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
data_to_insert.append((stock_code, stock_name, market_location, market_type, market_cap, updated_at))
# executemany를 사용하여 데이터 삽입
cursor.executemany(query, data_to_insert)
connection.commit()
print("Data Inserted Success")
except Exception as e:
print(f"An error occurred: {e}")
finally:
connection.close()