forked from cccbackup/sp
-
Notifications
You must be signed in to change notification settings - Fork 0
mid.md
Roy-Roo edited this page Jun 11, 2022
·
13 revisions
模組導入 :
import requests //網頁擷取
import pandas as pd //資料分析
import xlwings as xw //Excel表格處理
from datetime import date //處理日期
日期設定(設定成今日) :
today = date.today()
datestr = str(today).replace('-','')
下載股價 :
url = 'https://www.twse.com.tw/exchangeReport/MI_INDEX?response=csv&date=' + datestr + '&type=ALL'
res = requests.get(url)
data = res.text
整理資料,變成表格 :
cleaned_data = []
for da in data.split('\n'):
if len(da.split('","')) == 16 and da.split('","')[0][0] != '=':
cleaned_data.append([ele.replace('",\r','').replace('"','')
for ele in da.split('","')])
判斷今天是否是工作日 :
if cleaned_data == [] :
print("datestr +今天是休息日")
顯示(Excel檔案呈現) :
df = pd.DataFrame(cleaned_data, columns = cleaned_data[0])
df = df.set_index('證券代號')[1:]
xw.view(df)
完整程式碼 :
import requests
import pandas as pd
import xlwings as xw
from datetime import date
today = date.today()
datestr = str(today).replace('-','')
url = 'https://www.twse.com.tw/exchangeReport/MI_INDEX?response=csv&date=' + datestr + '&type=ALL'
res = requests.get(url)
data = res.text
cleaned_data = []
for da in data.split('\n'):
if len(da.split('","')) == 16 and da.split('","')[0][0] != '=':
cleaned_data.append([ele.replace('",\r','').replace('"','')
for ele in da.split('","')])
if cleaned_data == [] :
print(datestr +"今天是休息日")
else :
df = pd.DataFrame(cleaned_data, columns = cleaned_data[0])
df = df.set_index('證券代號')[1:]
xw.view(df)
執行結果(20220611與20220610當範例) :
-
20220611
-
20220610
改動部分 :
-
1.新增當日日期的擷取
-
2.資料有無的判斷
參考資料 :