跟昨天一樣至台灣證券交易所網站:https://www.twse.com.tw/zh/
進入網站後點選交易資訊/盤後資訊/每5秒委託成交統計
由於是每5秒提供一筆資料,因此下載後發現總共有3000多行,我們想辦法將棋改成每30分鐘取得一次累計資料。
import csv
fn = '/Users/sunwei/Desktop/iThone/Day23/MI_5MINS_20221007.csv'
out = '/Users/sunwei/Desktop/iThone/Day23/MI_30MINS_20221007.csv'
listCsv = list(csvReader)
csvData = listCsv[2:-8]
xmin = row[0][3:5]
xsec = row[0][6:]
if xmin == '00' or xmin == '30':
if xsec == '00':
csvWriter.writerow([row[0], row[6]])
程式碼:
import csv
fn = '/Users/sunwei/Desktop/iThone/Day23/MI_5MINS_20221007.csv'
out = '/Users/sunwei/Desktop/iThone/Day23/MI_30MINS_20221007.csv'
with open(out, 'w', newline='') as csvOut:
csvWriter = csv.writer(csvOut)
csvWriter.writerow(['時間', '累積成交數'])
with open(fn) as csvFile:
csvReader = csv.reader(csvFile)
listCsv = list(csvReader)
csvData = listCsv[2:-8]
for row in csvData:
xmin = row[0][3:5]
xsec = row[0][6:]
if xmin == '00' or xmin == '30':
if xsec == '00':
csvWriter.writerow([row[0], row[6]])
參考書籍:
洪錦魁 -- Python網路爬蟲:大數據擷取、清洗、儲存與分析:王者歸來 2019
林俊瑋, 林修博 --- Python:網路爬蟲與資料分析入門實戰 2018