我正在做一个个人项目——更多的是一个学习冒险,这个项目包括一些在 python 中可能看起来很常见的数据争论任务。我已经尝试了多种途径来自行解决或使用从其他线程收集的信息,但还没有运气。
一些背景:我正在尝试获取股票代码的列表或数据框并传递到 yfinance 的代码模块中,以提取大量股票的历史记录。听起来很简单 - 可能适合使用 Python 一段时间的人。
我的脚本:
import yfinance as yf
import pandas as pd
from get_all_tickers import get_tickers as gt
list_of_tickers = gt.get_tickers()
tickerlist = pd.DataFrame(list_of_tickers, columns = ["Ticker"])
tickerlist = tickerlist.values.tolist()
tc = ""
for x in tickerlist:
tc += str(x)
tc = tc.replace(" ", "")
tc = tc.replace("[", ",")
tc = tc.replace("]", "")
tc = tc.replace("'", "")
#tc = ",".join(tc)
def Convert(string):
li = list(string.split(","))
return li
tc = Convert(tc)
df = pd.DataFrame(data=tc, columns = ["Ticker"])
df_filtered = df.iloc[1: , : ]
#df_filtered.update('"' + df_filtered[['Ticker']].astype(str) + '"')
# df_filtered.to_csv("./StockTickers.csv", sep=',',index=False)
list_filtered = df_filtered.values.tolist()
# valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max
period= "1d"
# valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h,1d,5d,1wk,1mo,3mo
interval = "1d"
#=============================================================================
for ticker in df_filtered: #df_filtered.iterrows():
ticker = str(ticker).upper()
ticker = yf.Ticker("DDD")
history = ticker.history(
period = period,
interval = interval
)
print(history)
print(df_filtered)