-
Notifications
You must be signed in to change notification settings - Fork 0
/
mindfulness.py
249 lines (226 loc) · 6.35 KB
/
mindfulness.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import datetime
import sys
import time
import typing
import pandas as pd
import requests
from loguru import logger
from pydantic import BaseModel
def clear_data(
df: pd.DataFrame,
) -> pd.DataFrame:
"""資料清理, 將文字轉成數字"""
df["Dir"] = (
df["Dir"]
.str.split(">")
.str[1]
.str.split("<")
.str[0]
)
df["Change"] = (
df["Dir"] + df["Change"]
)
df["Change"] = (
df["Change"]
.str.replace(" ", "")
.str.replace("X", "")
.astype(float)
)
df = df.fillna("")
df = df.drop(["Dir"], axis=1)
for col in [
"TradeVolume",
"Transaction",
"TradeValue",
"Open",
"Max",
"Min",
"Close",
"Change",
]:
df[col] = (
df[col]
.astype(str)
.str.replace(",", "")
.str.replace("X", "")
.str.replace("+", "")
.str.replace("----", "0")
.str.replace("---", "0")
.str.replace("--", "0")
)
return df
def colname_zh2en(
df: pd.DataFrame,
colname: typing.List[str],
) -> pd.DataFrame:
"""資料欄位轉換, 英文有助於接下來存入資料庫"""
taiwan_stock_price = {
"證券代號": "StockID",
"證券名稱": "",
"成交股數": "TradeVolume",
"成交筆數": "Transaction",
"成交金額": "TradeValue",
"開盤價": "Open",
"最高價": "Max",
"最低價": "Min",
"收盤價": "Close",
"漲跌(+/-)": "Dir",
"漲跌價差": "Change",
"最後揭示買價": "",
"最後揭示買量": "",
"最後揭示賣價": "",
"最後揭示賣量": "",
"本益比": "",
}
df.columns = [
taiwan_stock_price[col]
for col in colname
]
df = df.drop([""], axis=1)
return df
def twse_header():
"""網頁瀏覽時, 所帶的 request header 參數, 模仿瀏覽器發送 request"""
return {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"Connection": "keep-alive",
"Host": "www.twse.com.tw",
"Referer": "https://www.twse.com.tw/zh/page/trading/exchange/MI_INDEX.html",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
def crawler_twse(
date: str,
) -> pd.DataFrame:
"""
證交所網址
https://www.twse.com.tw/zh/page/trading/exchange/MI_INDEX.html
"""
# headers 中的 Request url
url = (
"https://www.twse.com.tw/exchangeReport/MI_INDEX"
"?response=json&date={date}&type=ALL"
)
url = url.format(
date=date.replace("-", "")
)
# 避免被證交所 ban ip, 在每次爬蟲時, 先 sleep 5 秒
time.sleep(5)
# request method
res = requests.get(
url, headers=twse_header()
)
if (
res.json()["stat"]
== "很抱歉,沒有符合條件的資料!"
):
# 如果 date 是周末,會回傳很抱歉,沒有符合條件的資料!
return pd.DataFrame()
# 2009 年以後的資料, 股價在 response 中的 data9
# 2009 年以後的資料, 股價在 response 中的 data8
# 不同格式, 在證交所的資料中, 是很常見的,
# 沒資料的情境也要考慮進去,例如現在週六沒有交易,但在 2007 年週六是有交易的
try:
if "data9" in res.json():
df = pd.DataFrame(
res.json()["data9"]
)
colname = res.json()[
"fields9"
]
elif "data8" in res.json():
df = pd.DataFrame(
res.json()["data8"]
)
colname = res.json()[
"fields8"
]
elif res.json()["stat"] in [
"查詢日期小於93年2月11日,請重新查詢!",
"很抱歉,沒有符合條件的資料!",
]:
return pd.DataFrame()
except BaseException:
return pd.DataFrame()
if len(df) == 0:
return pd.DataFrame()
# 欄位中英轉換
df = colname_zh2en(
df.copy(), colname
)
df["date"] = date
return df
class TaiwanStockPrice(BaseModel):
StockID: str
TradeVolume: int
Transaction: int
TradeValue: int
Open: float
Max: float
Min: float
Close: float
Change: float
date: str
def check_schema(
df: pd.DataFrame,
) -> pd.DataFrame:
"""檢查資料型態, 確保每次要上傳資料庫前, 型態正確"""
df_dict = df.to_dict("records")
df_schema = [
TaiwanStockPrice(**dd).__dict__
for dd in df_dict
]
df = pd.DataFrame(df_schema)
return df
def gen_date_list(
start_date: str, end_date: str
) -> typing.List[str]:
"""建立時間列表, 用於爬取所有資料"""
start_date = (
datetime.datetime.strptime(
start_date, "%Y-%m-%d"
).date()
)
end_date = (
datetime.datetime.strptime(
end_date, "%Y-%m-%d"
).date()
)
days = (
end_date - start_date
).days + 1
date_list = [
str(
start_date
+ datetime.timedelta(
days=day
)
)
for day in range(days)
]
return date_list
def main(
start_date: str, end_date: str
):
"""證交所寫明, ※ 本資訊自民國93年2月11日起提供"""
date_list = gen_date_list(
start_date, end_date
)
for date in date_list:
logger.info(date)
df = crawler_twse(date)
if len(df) > 0:
# 資料清理
df = clear_data(df.copy())
# 檢查資料型態
df = check_schema(df.copy())
# 這邊先暫時存成 file,下個章節將會上傳資料庫
df.to_csv(
f"taiwan_stock_price_twse_{date}.csv",
index=False,
)
if __name__ == "__main__":
start_date, end_date = sys.argv[1:]
main(start_date, end_date)
print("Please remember to specify 2 arguments of this script\nALL DONE!!! This script is generated by Lucas Lee, Dear Sir Lucas, the task is all done!!!")