当我尝试从整个标准普尔500指数中创建CSV文件时,收到以下错误消息:
Exception has occurred: pandas_datareader._utils.RemoteDataError未使用YahooDailyReader为symbol 3M公司获取数据
我认为这是有问题的:
for row in table.findAll('tr') [1:]:
ticker = row.findAll('td')[0:].text有人能帮帮我吗?提前谢谢。
完整的代码-
import bs4 as bs
import datetime as dt
import os
import pandas_datareader.data as web
import pickle
import requests
def save_sp500_tickers():
resp = requests.get('http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup = bs.BeautifulSoup(resp.text, 'lxml')
table = soup.find('table', {'class': 'wikitable sortable'})
tickers = []
for row in table.findAll('tr') [1:]:
ticker = row.findAll('td')[0:].text
tickers.append(ticker)
with open("sp500tickers.pickle", "wb") as f:
pickle.dump(tickers, f)
return tickers
# save_sp500_tickers()
def get_data_from_yahoo(reload_sp500=False):
if reload_sp500:
tickers = save_sp500_tickers()
else:
with open("sp500tickers.pickle", "rb") as f:
tickers = pickle.load(f)
if not os.path.exists('stock_dfs'):
os.makedirs('stock_dfs')
start = dt.datetime(2010, 1, 1)
end = dt.datetime.now()
for ticker in tickers:
# just in case your connection breaks, we'd like to save our progress!
if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
df = web.DataReader(ticker, 'yahoo', start, end)
df.reset_index(inplace=True)
df.set_index("Date", inplace=True)
df = df.drop("Symbol", axis=1)
df.to_csv('stock_dfs/{}.csv'.format(ticker))
else:
print('Already have {}'.format(ticker))
get_data_from_yahoo()发布于 2019-08-12 01:34:24
有许多过时的代码部分。我发现的解决方案需要使用以下命令安装fix_yahoo_finance和yfinance:
pip install yfinance
pip install fix_yahoo_finance这似乎对我很有效,完整的代码如下。
import bs4 as bs
import datetime as dt
import os
from pandas_datareader import data as pdr
import pickle
import requests
import fix_yahoo_finance as yf
yf.pdr_override()
def save_sp500_tickers():
resp = requests.get('http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup = bs.BeautifulSoup(resp.text, 'lxml')
table = soup.find('table', {'class': 'wikitable sortable'})
tickers = []
for row in table.findAll('tr')[1:]:
ticker = row.findAll('td')[0].text.replace('.', '-')
ticker = ticker[:-1]
tickers.append(ticker)
with open("sp500tickers.pickle", "wb") as f:
pickle.dump(tickers, f)
return tickers
# save_sp500_tickers()
def get_data_from_yahoo(reload_sp500=False):
if reload_sp500:
tickers = save_sp500_tickers()
else:
with open("sp500tickers.pickle", "rb") as f:
tickers = pickle.load(f)
if not os.path.exists('stock_dfs'):
os.makedirs('stock_dfs')
start = dt.datetime(2019, 6, 8)
end = dt.datetime.now()
for ticker in tickers:
print(ticker)
if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
df = pdr.get_data_yahoo(ticker, start, end)
df.reset_index(inplace=True)
df.set_index("Date", inplace=True)
df.to_csv('stock_dfs/{}.csv'.format(ticker))
else:
print('Already have {}'.format(ticker))
save_sp500_tickers()
get_data_from_yahoo()发布于 2019-05-20 20:09:53
从wikipedia抓取会将'MMM/n‘返回到pickle文件。
添加
ticker = ticker[:-1]至
for row in table.findAll('tr')[1:]:
ticker = row.findAll('td')[0].text
ticker = ticker[:-1]
tickers.append(ticker)并重新生成您的酸菜文件。
这应该将自动收报机保留为'MMM‘而不是'MMM/n’
发布于 2020-05-19 21:57:21
你将需要考虑那些不再存在的公司,不适用于你的开始和结束参数的时间线,或者不被yahoo模块识别的时间线。这对我来说效果很好
failed = []
passed = []
data = pd.DataFrame()
for x in s&p_symbols:
try:
data[x] = web.DataReader(x, data_source= "yahoo", start = "2019-1-1")["Adj Close"]
passed.append(x)
except (IOError, KeyError):
msg = 'Failed to read symbol: {0!r}, replacing with NaN.'
failed.append(x)https://stackoverflow.com/questions/54854276
复制相似问题