所以我正在运行我的代码,你可以在下面看到。此代码从yahoo api中提取数据并将所述数据作为CSV返回给我的计算机。但是它通过大约一半而且它停在LTM为什么会这样,我怎么能解决它......如果有人知道这个答案非常赞赏 - >我如何每天更新这些CSV任何简单的解决方案。
错误 - >引发RemoteDataError('无法读取URL:{0}' .format(url)) pandas_datareader._utils.RemoteDataError:无法读取URL:http://ichart.finance.yahoo.com/table.csv?ignore=.csv&e=31&g=d&s=LMT&b=1&a=0&c=2000&d=11&f=2016如果您查看URL的中间,您会看到股票代码LMT。
import os
import datetime as dt
import pandas as pd
import pandas_datareader.data as web
import bs4 as bs
import pickle
import requests
def save_sp500_tickers():
resp = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup = bs.BeautifulSoup(resp.text, 'html.parser')
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)
print(tickers)
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(2000, 1, 1)
end = dt.datetime(2016, 12, 31)
for ticker in tickers:
print(ticker)
# 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.to_csv('stock_dfs/{}.csv'.format(ticker))
else:
print('Already have {}'.format(ticker))
get_data_from_yahoo()