-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDay18_saveApiStockToDb.py
100 lines (86 loc) · 3.19 KB
/
Day18_saveApiStockToDb.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import requests
import pandas
import pyodbc
import json
def convert_date(data_ROC):
"""因為格式為109/1/1,為民國年,需轉換成西元年"""
date_arr = data_ROC.split("/")
new_year = int(date_arr[0]) + 1911
return f"{new_year}-{date_arr[1]}-{date_arr[2]}"
def create_new_header(orignal_headers):
new_headers = []
for column in orignal_headers:
data = str(column)
if data == "日期":
new_headers.append("trade_date")
elif data == "證券名稱":
new_headers.append("stock_name")
elif data == "成交股數":
new_headers.append("volume")
elif data == "成交金額":
new_headers.append("total_price")
elif data == "開盤價":
new_headers.append("open")
elif data == "最高價":
new_headers.append("high")
elif data == "最低價":
new_headers.append("low")
elif data == "收盤價":
new_headers.append("close")
elif data == "漲跌價差":
new_headers.append("spread")
elif data == "成交筆數":
new_headers.append("transactions_number")
return new_headers
def save_data_to_azure_db(symbol, tock_data):
server = "ey-finance.database.windows.net"
database = "finance"
username = "我的帳號"
password = "我的密碼"
driver = "{ODBC Driver 17 for SQL Server}"
with pyodbc.connect(
f"DRIVER={driver};SERVER={server};PORT=1433;DATABASE={database};UID={username};PWD={password}"
) as conn:
with conn.cursor() as cursor:
# 把Dataframe 匯入到SQL Server:
for index, row in df.iterrows():
try:
cursor.execute(
"""INSERT INTO finance.dbo.DailyPrice
(StockID, Symbol, TradeDate, OpenPrice, HighPrice,
LowPrice, ClosePrice, Volumn)
values(?,?,?,?,?,?,?,?);""",
"",
symbol,
convert_date(row.trade_date),
row.open,
row.high,
row.low,
row.close,
int(row.volume.replace(",", "")),
)
conn.commit()
# if index % 10 == 0:
# time.sleep(1)
except Exception as e:
print(e)
return True
return False
# API位置
# address = "http://www.twse.com.tw/exchangeReport/STOCK_DAY_ALL?response=open_data"
stock = "2330"
date = "202009"
address = f"https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date={date}&stockNo={stock}"
# 取得資料
response = requests.get(address)
# 解析
# 有幾個部分:stat, date, title, fields, data, notes
data = response.text # 這是json格式的資料
a_json = json.loads(data) # 轉成dict
df = pandas.DataFrame.from_dict(a_json["data"]) # 轉成dataframe
# 修改欄位名稱
new_headers = create_new_header(a_json["fields"])
df.columns = new_headers # 設定資料欄位的名稱
# print(df)
_ = save_data_to_azure_db(stock, df)
print("===finished===")