http://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=20160501&stockNo=2330
比較重要的地方是date這個參數, 基本上你給的值一定要是yyyyMMdd的形式, 但是真正作用的只有yyyy與MM,
因為他會把這段request解讀成你想要看stockNo股票在yyyy年MM月的紀錄, 所以dd基本上沒有太大意義, 但卻是不可少的部分.
- import requests
- import time
- TWSE_URL = 'http://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json'
- def get_web_content(stock_id, current_date):
- resp = requests.get(TWSE_URL + '&date=' + current_date + '&stockNo=' + stock_id)
- if resp.status_code != 200:
- return None
- else:
- return resp.json()
- def get_data(stock_id, current_date):
- info = list()
- resp = get_web_content(stock_id, current_date)
- if resp is None:
- return None
- else:
- if resp['data']:
- for data in resp['data']:
- record = {
- '日期': data[0],
- '開盤價': data[3],
- '收盤價': data[6],
- '成交筆數': data[8]
- }
- info.append(record)
- return info
- def main():
- stock_id = '2330'
- current_date = time.strftime('%Y%m%d')
- current_year = time.strftime('%Y')
- current_month = time.strftime('%m')
- print('Processing data for %s %s...' % (current_year, current_month))
- get_data(stock_id, current_date)
- collected_info = get_data(stock_id, current_date)
- for info in collected_info:
- print(info)
- if __name__ == '__main__':
- main()
複製代碼 Processing data for 2023 04...
{'日期': '112/04/06', '開盤價': '530.00', '收盤價': '530.00', '成交筆數': '19,144'}
{'日期': '112/04/07', '開盤價': '535.00', '收盤價': '531.00', '成交筆數': '10,285'}
{'日期': '112/04/10', '開盤價': '533.00', '收盤價': '529.00', '成交筆數': '11,698'}
{'日期': '112/04/11', '開盤價': '522.00', '收盤價': '524.00', '成交筆數': '24,432'}
{'日期': '112/04/12', '開盤價': '523.00', '收盤價': '520.00', '成交筆數': '37,158'}
{'日期': '112/04/13', '開盤價': '515.00', '收盤價': '510.00', '成交筆數': '64,834'}
{'日期': '112/04/14', '開盤價': '516.00', '收盤價': '516.00', '成交筆數': '19,962'}
{'日期': '112/04/17', '開盤價': '519.00', '收盤價': '520.00', '成交筆數': '17,444'}
{'日期': '112/04/18', '開盤價': '518.00', '收盤價': '515.00', '成交筆數': '17,087'}
{'日期': '112/04/19', '開盤價': '517.00', '收盤價': '510.00', '成交筆數': '43,281'}
{'日期': '112/04/20', '開盤價': '512.00', '收盤價': '513.00', '成交筆數': '16,612'}
|