【发布时间】:2020-07-20 12:39:17
【问题描述】:
我在 python 上使用 Yahoo Financials 包来检索股票数据。我有一个大约 30 只我感兴趣的股票的列表,我编写了一个脚本来循环遍历该列表,提取收盘价并使用该数据构建一个数据框。
但是,我遇到以下 TimeoutError:
[WinError 10060] 连接尝试失败,因为连接的一方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应。
它还指出在处理上述情况时发生了另一个错误:
OSError: [Errno socket error] [WinError 10060] 连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应。
我的代码如下:
stistocks = ["U96.SI", "D01.SI", "J36.SI", "O39.SI", "BN4.SI", "N2IU.SI", "BS6.SI", "G13.SI", "V03.SI", "S63.SI", "F34.SI", "S68.SI", "C52.SI", "Z74.SI",
"A17U.SI", "U11.SI", "H78.SI", "M44U.SI", "C31.SI", "U14.SI", "J37.SI", "T39.SI", "C6L.SI", "S58.SI", "D05.SI", "C38U.SI", "C09.SI", "C61U.SI", "C07.SI", "Y92.SI", "A35.SI"]
factor = 0
for ticker in stistocks:
yahoo_financials = YahooFinancials(ticker)
historical_data = yahoo_financials.get_historical_price_data('2000-01-01', '2020-07-17', 'daily')
historical_stock_prices = historical_data[ticker]["prices"]
date_list = [each_day["formatted_date"] for each_day in historical_stock_prices]
price_list = [each_day["adjclose"] for each_day in historical_stock_prices]
if factor == 0:
data = {"Date": date_list, ticker:price_list}
df_first = pd.DataFrame.from_dict(data)
df_first["Date"] = pd.to_datetime(df_first["Date"], format="%Y-%m-%d")
factor = factor + 1
else:
data = {"Date": date_list, ticker:price_list}
df = pd.DataFrame.from_dict(data)
df["Date"] = pd.to_datetime(df["Date"], format="%Y-%m-%d")
df_first = df_first.merge(df, on="Date", how='outer')
【问题讨论】: