#!/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 import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # # Part 1: 下載一個月台股個股成交資料 # In[2]: url = "https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=20200801&stockNo=2330" # 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: 下載歷年台股個股成交資料 # In[13]: url = "https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=20200801&stockNo=2330" # 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/STOCK_DAY?response=json&date="+ month + "&stockNo=2330" 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, stock_code): 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/STOCK_DAY?response=json&date="+ month + "&stockNo=" + str(stock_code) 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 # In[21]: get_stock_data(start_year = 2019, start_month = 6, end_year = 2019, end_month = 8, stock_code = 2330) # # Part 3: 將下載的個股資料從字串(string)轉型成日期(datetime)及浮點數(float) # In[22]: stock = get_stock_data(start_year = 2019, start_month = 6, end_year = 2019, end_month = 8, stock_code = 2330) stock.head() # ### 把日期從字串(string)換成時間(datetime),並將民國年換成西元年 # In[23]: for row in range(stock.shape[0]): date2 = stock.iloc[row,0].split('/') stock.iloc[row, 0] = datetime(int(date2[0]) + 1911, int(date2[1]), int(date2[2])) stock.head(10) # ### 把"成交股數" "成交金額" "開盤價", "最高價", "最低價", "收盤價" "漲跌價差" "成交筆數"帶有逗號的字串(string)換成浮點數(float) # In[26]: for col in [1, 2, 3, 4, 5, 6, 8]: for row in range(stock.shape[0]): stock.iloc[row, col] = float(stock.iloc[row,col].replace(',', '')) stock.head(10) # # Part 4: 將下載的個股資料另存成csv檔 # In[27]: stock.to_csv("2330.csv") # # Part 5: 畫出下載個股的走勢圖 # In[28]: stock2 = stock[:10] # In[29]: stock2 # In[30]: fig = plt.figure(figsize = (10, 5)) plt.title('2330 Stock Price') plt.plot(stock2['日期'], stock2['收盤價']) plt.plot(stock2['日期'], stock2['開盤價']) plt.legend(['Close', 'Open']) # In[ ]: