之前在如何撈取台美股所有的股號的資料並儲存於SQLite? Part 1討論到如何使用stocksymbol撈取台美股股票代碼,在這篇我們介紹另一種方法撈取清單。
如果僅是要獲取美股清單,FinanceDatabase會是另一種方法,儘管無法獲取台股清單,但financedatabase裡有包含ETF的資訊,這些資訊是stocksymbol裡沒有的。
步驟1: 安裝financedatabase。
!pip install financedatabase
步驟2: 獲取股票清單,並將Inde轉為欄位。
import financedatabase as fd
import pandas as pd
equities = fd.Equities()
df = equities.select()
df['symbol'] = df.index
步驟3: 獲取ETF清單,並將Inde轉為欄位。
etfs = fd.ETFs()
df_etf = etfs.select()
df_etf ['symbol'] = df_etf.index
步驟4: 將此兩個DataFrame輸入於SQLite中。
from google.colab import drive
drive.mount('/content/drive')
df = df.reset_index(drop=True)
df_etf = df_etf.reset_index(drop=True)
import sqlite3
con = sqlite3.connect('/content/drive/MyDrive/data/Stock.db')
df.to_sql('financedatabase_eq', con, if_exists='replace')
df_etf.to_sql('financedatabase_etf', con, if_exists='replace')
con.close()
Thank you and Enjoy it! You can check this for English version!
If you want to support Informula, you can buy us a coffee here :)
Thank you and more to come :)