今天利用API試著從 twse 撈出資料
curl https://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_0056.tw|jq
{
"msgArray": [
{
"tv": "-",
"ps": "-",
"nu": "http://www.yuantaetfs.com/#/RtNav/Index",
"pz": "-",
"bp": "0",
"a": "34.3800_34.3900_34.4000_34.4100_34.4200_",
"b": "34.3700_34.3600_34.3500_34.3400_34.3300_",
"c": "0056",
"d": "20210714",
"ch": "0056.tw",
"tlong": "1626229079000",
"f": "446_1402_1414_406_48_",
"ip": "0",
"g": "70_209_677_95_78_",
"mt": "848857",
"h": "34.9000",
"it": "02",
"l": "34.3500",
"n": "元大高股息",
"o": "34.8900",
"p": "0",
"ex": "tse",
"s": "-",
"t": "10:17:59",
"u": "38.3300",
"v": "14561",
"w": "31.3700",
"nf": "元大臺灣高股息證券投資信託基金",
"y": "34.8500",
"z": "-",
"ts": "0"
}
],
"referer": "",
"userDelay": 5000,
"rtcode": "0000",
"queryTime": {
"sysDate": "20210714",
"stockInfoItem": 901,
"stockInfo": 188897,
"sessionStr": "UserSession",
"sysTime": "10:18:03",
"showChart": false,
"sessionFromTime": -1,
"sessionLatestTime": -1
},
"rtmessage": "OK",
"exKey": "if_tse_0056.tw_zh-tw.null",
"cachedAlive": 82099
}
如果要取得msgArray裡的資料
指令如下
curl https://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_00882.tw|jq '.msgArray'
[
{
"tv": "-",
"ps": "-",
"nu": "https://www.ctbcinvestments.com/Product/ETFBusiness",
"pz": "-",
"bp": "0",
"a": "15.4100_15.4200_15.4300_15.4400_15.4500_",
"b": "15.4000_15.3900_15.3800_15.3700_15.3600_",
"c": "00882",
"d": "20210714",
"ch": "00882.tw",
"tlong": "1626229529000",
"f": "1005_1039_946_454_514_",
"ip": "0",
"g": "3086_873_1726_575_596_",
"mt": "353051",
"h": "15.4300",
"it": "02",
"l": "15.4000",
"n": "中信中國高股息",
"o": "15.4300",
"p": "0",
"ex": "tse",
"s": "-",
"t": "10:25:29",
"u": "9999.9500",
"v": "17761",
"nf": "中國信託全球收益ETF傘型證券投資信託基金之中國信託恒生中國高股息ETF證券投資信託基金",
"y": "15.4500",
"z": "-",
"ts": "0"
}
]
再來要報得目前的即時報價 在 "a" 這個欄位 這裡花了一點時間試
因為上一步的資料裡多了中括號 [ ]
所以指令要改成如下
curl https://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch=tse_0056.tw|jq '.msgArray[].a'
"34.3700_34.3800_34.3900_34.4000_34.4100_"
第一個分隔就是目前報價
https://zys-notes.blogspot.com/2020/01/api.html