用python寫一支可以取得5秒加權指數歷史資料的程式
import requests
import pandas as pd
import os
# 這個函數用來判斷要不要跳過某一行
def skip_rows(row):
return row.startswith("說明:") or row.startswith("民國")
def download_taiex_csv(date):
print(date+"下載轉換中")
url = f'https://www.twse.com.tw/exchangeReport/MI_5MINS_INDEX?response=csv&date={date}&type=csv'
response = requests.get(url)
tmpfilename = f'tmp{date}.csv'
with open(tmpfilename, 'w') as f:
f.write(response.text)
try:
df = pd.read_csv(tmpfilename, skiprows=1)
except ValueError:
print(f'No data available for {date}')
else:
df = df[['時間', '發行量加權股價指數']]
df.rename(columns={'發行量加權股價指數': '加權指數'}, inplace=True)
df['時間'] = df['時間'].apply(lambda x: x.replace('=', '').replace('"', ''))
df['時間'] = pd.to_datetime(df['時間'], errors='coerce')
df['時間'] = df['時間'].dt.strftime('%H:%M:%S')
df['加權指數'] = df['加權指數'].str.replace(',', '').astype(float)
df = df.dropna(subset=['時間'])
print(df.head())
# 儲存為CSV檔案
df.to_csv('taiex_%s.csv' % date, index=False)
os.remove(tmpfilename)
# 主程式
start_date = '20230329'
end_date = '20230401'
for date in pd.date_range(start_date, end_date):
download_taiex_csv(date.strftime('%Y%m%d'))
沒有留言:
張貼留言