#!/usr/bin/env python # coding: utf-8 # # Part 0: Import package # In[1]: import requests as r import json import pandas as pd from datetime import datetime, date # # Part 1: Download 1 month data in TAIEX (台股 加權股價指數) # In[2]: url = "https://www.twse.com.tw/exchangeReport/FMTQIK?response=json&date=20200701" # In[3]: r.get(url) # In[4]: res = r.get(url) # In[5]: res.json() # In[6]: stock_json = res.json() # In[7]: stock_json['data'] # In[8]: pd.DataFrame.from_dict(stock_json['data']) # In[9]: stock_df = pd.DataFrame.from_dict(stock_json['data']) stock_df.head() # In[10]: stock_json['fields'] # In[11]: stock_df.columns = stock_json['fields'] # In[12]: stock_df.head() # # Part 2: Download multi months data in TAIEX (台股 加權股價指數) # In[13]: url = "https://www.twse.com.tw/exchangeReport/FMTQIK?response=json&date=20200701" # In[14]: month_list = pd.date_range('2020-06-01','2020-08-01', freq='MS').strftime("%Y%m%d").tolist() # In[15]: for month in month_list: print(month) # In[16]: df = pd.DataFrame() df # In[17]: for month in month_list: url = "https://www.twse.com.tw/exchangeReport/FMTQIK?response=json&date=" + month res = r.get(url) stock_json = res.json() stock_df = pd.DataFrame.from_dict(stock_json['data']) df = df.append(stock_df, ignore_index = True) # In[18]: df.columns = ['日期', '成交股數', '成交金額', '成交筆數', '發行量加權股價指數', '漲跌點數'] # In[19]: df.head() # In[20]: def get_stock_data(start_year, start_month, end_year, end_month): start_date = str(date(start_year, start_month, 1)) end_date = str(date(end_year, end_month, 1)) month_list = pd.date_range(start_date, end_date, freq='MS').strftime("%Y%m%d").tolist() df = pd.DataFrame() for month in month_list: url = "https://www.twse.com.tw/exchangeReport/FMTQIK?response=json&date=" + month res = r.get(url) stock_json = res.json() stock_df = pd.DataFrame.from_dict(stock_json['data']) df = df.append(stock_df, ignore_index = True) df.columns = ['日期', '成交股數', '成交金額', '成交筆數', '發行量加權股價指數', '漲跌點數'] return df # # Part 3: Save the dataframe as csv file # In[21]: get_stock_data(start_year = 2019, start_month = 7, end_year = 2019, end_month = 9) # In[22]: stock = get_stock_data(start_year = 2019, start_month = 7, end_year = 2019, end_month = 9) stock.head() # In[23]: stock.to_csv("TAIEX.csv") # In[ ]: