I was following your tutorial on python programming for finance. On video #6 I did your code and it started working but after 20 companies I get a Remote Data Error. Please let me know if there is something you can help me with to fix this error. Thanks
Regards,
Code:
import bs4 as bs import datetime as dt import os import pandas as pd import pandas_datareader.data as web import pickle #serializes any python object import requests
# Ctrl + U to see source code def save_sp500_tickers(): resp = requests.get('https://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)
print(tickers) return tickers
save_sp500_tickers()
#Getting the data from the companies stored
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)
# grab tickers[:,25]
for ticker in tickers: 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()
ERROR:
File "C:UsersAlbertoAnaconda3libsite-packagespandas_datareaderbase.py", line 139, in _get_response raise RemoteDataError('Unable to read URL: {0}'.format(url))
RemoteDataError: Unable to read URL: https://query1.finance.yahoo.com/v7/finance/download/ALLE?period1=946710000&period2=1483253999&interval=1d&events=history&crumb=2m%5Cu002FGjqNQIoI
You must be logged in to post. Please login or register an account.