“”” 台灣證交所的網址:
https://www.twse.com.tw/exchangeReport/STOCK_DAY?date=%04d%02d01&stockNo=%s
%04d要換成年,如: 2022
%02d要換成月,如: 09
#語法是%02d,非%2d,
#前者遇到一位數字,前方會自動補0
%s要換成股票代號,如: “2330”
#以字串形式帶入
可以參考台灣證交所API
“””
import sys
def URLGet(stockNo: str,
year: int, month: int,
targetFile: str) -> bool:
import sys
import requests
import os
ret = False
try:
if os.path.exists(targetFile): # 注意是exists,結尾有s
raise RuntimeError(“File Exists: %s”
% targetFile)
# raise RuntimeError?
inputs = \
“https://www.twse.com.tw/exchangeReport/STOCK_DAY?date=%04d%02d01&stockNo=%s” \
% (year, month, stockNo)
#%02d才正確,前面會補0,
#使用%2d, 0的位置會變空白,網址會在該處斷開
print(“URL = %s” % inputs)
rawData = requests.get(inputs)
print(“requests.get: \n”, rawData) #<Response [200]>
if rawData.status_code == 200: # 200表示正常
print(“The server of twse:”,
rawData.headers[“Server”])
with open(targetFile, “w”) as f:
f.write(rawData.text)
ret = True
except BaseException as e:
print(e, file=sys.stderr) # file=sys.stderr?
finally:
pass
return ret
# =========main=======
if __name__ == “__main__”:
whereStore = input(“請輸入下載路徑:\t”)
print(URLGet(“2330”, 2022, 9, whereStore))
sys.exit(0)
輸出結果:
電腦中寫入2330.txt
內容如下:
{“stat”:”OK”,”date”:”20220901″,”title”:”111年09月 2330 台積電 各日成交資訊”,”fields”:[“日期”,”成交股數”,”成交金額”,”開盤價”,”最高價”,”最低價”,”收盤價”,”漲跌價差”,”成交筆數”],”data”:[[“111/09/01″,”42,008,490″,”20,696,930,527″,”495.00″,”495.50″,”490.00″,”490.50″,”-14.50″,”93,631″],[“111/09/02″,”33,877,655″,”16,486,942,365″,”488.00″,”489.50″,”485.00″,”485.00″,”-5.50″,”71,175″],[“111/09/05″,”17,327,720″,”8,419,762,666″,”485.00″,”488.00″,”484.00″,”486.00″,”+1.00″,”27,611”],[“111/09/06″,”21,197,718″,”10,364,807,027″,”488.50″,”491.50″,”486.50″,”489.00″,”+3.00″,”18,950”],[“111/09/07″,”38,168,331″,”18,125,281,029″,”477.00″,”478.00″,”472.00″,”472.50″,”-16.50″,”101,077″],[“111/09/08″,”35,355,467″,”16,755,699,888″,”473.00″,”475.00″,”472.00″,”475.00″,”+2.50″,”37,943”],[“111/09/12″,”27,181,969″,”13,268,334,545″,”485.00″,”491.00″,”485.00″,”486.50″,”+11.50″,”26,764”],[“111/09/13″,”18,916,557″,”9,329,113,594″,”495.00″,”495.00″,”491.00″,”493.00″,”+6.50″,”20,511”],[“111/09/14″,”25,046,110″,”12,023,421,519″,”478.50″,”482.50″,”476.00″,”480.00″,”-13.00″,”47,333″],[“111/09/15″,”24,272,164″,”11,587,112,712″,”476.00″,”480.00″,”476.00″,”476.50″,”X0.00″,”35,435”],[“111/09/16″,”41,889,816″,”19,737,743,551″,”470.50″,”472.00″,”469.00″,”472.00″,”-4.50″,”51,619″],[“111/09/19″,”20,724,347″,”9,713,676,332″,”472.00″,”473.00″,”466.50″,”467.00″,”-5.00″,”45,650″],[“111/09/20″,”23,269,759″,”11,027,746,136″,”470.50″,”478.00″,”470.00″,”476.50″,”+9.50″,”20,621”],[“111/09/21″,”24,686,050″,”11,641,957,557″,”473.50″,”475.50″,”468.50″,”471.00″,”-5.50″,”30,847″],[“111/09/22″,”31,417,685″,”14,524,615,830″,”462.00″,”468.00″,”459.00″,”464.50″,”-6.50″,”53,974″],[“111/09/23″,”32,804,066″,”15,005,076,330″,”458.50″,”460.50″,”455.00″,”455.00″,”-9.50″,”82,492″],[“111/09/26″,”34,856,957″,”15,560,487,370″,”443.00″,”454.00″,”443.00″,”446.50″,”-8.50″,”75,861″],[“111/09/27″,”29,570,005″,”13,260,363,913″,”449.00″,”451.50″,”446.00″,”448.00″,”+1.50″,”29,995”],[“111/09/28″,”37,827,459″,”16,693,442,543″,”447.00″,”449.00″,”438.00″,”438.00″,”-10.00″,”78,780″],[“111/09/29″,”42,884,163″,”18,748,910,795″,”443.00″,”443.50″,”432.00″,”435.00″,”-3.00″,”70,971″],[“111/09/30″,”58,366,820″,”24,750,796,363″,”424.50″,”427.50″,”422.00″,”422.00″,”-13.00″,”87,052″]],”notes”:[“符號說明:+/-/X表示漲/跌/不比價”,”當日統計資訊含一般、零股、盤後定價、鉅額交易,不含拍賣、標購。”,”ETF證券代號第六碼為K、M、S、C者,表示該ETF以外幣交易。”]}
近期留言