I'm just writing code from this video in YouTube and I'm stumped on this error:
Traceback (most recent call last):
File "C:/Users/john/pycharm_coding/stock_investing_using_AI/stock investing/do
now/scripts/finance.py", line 49, in <module>
get_data_from_yahoo()
File "C:/Users/john/pycharm_coding/stock_investing_using_AI/stock investing/do now/scripts/finance.py", line 31, in get_data_from_yahoo
tickers = pickle.load(f)
EOFError: Ran out of input
I checked the code and everything is the same(unless you count the variable names), so I'm really puzzled.
Here is my code:
import bs4 as bs
import datetime as dt
import os
import pandas as pd
import pandas_datareader.data as web
import pickle
import requests
from datetime import date
link = 'https://www.youtube.com/watch?v=baCAFPHb1o4'
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()
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_data'):
os.makedirs('stock_data')
start = dt.datetime(2000, 1, 1)
end = date.today()
for ticker in tickers:
print(ticker)
if not os.path.exists('stock_data/{}.csv'.format(ticker)):
print('Scraping data from' + ticker + '...')
data = web.get_data_yahoo(ticker, 'yahoo', start, end)
data.to_csv('stock_data/{}.csv'.format(ticker))
print('Done.')
else:
print('Already have {}'.format(ticker))
get_data_from_yahoo()
I searched the web but I found only one result that matched my case, but it was a not-so-helpful one, and just in case you want to know what it is, here's the link.
At first, I tried copying the code and pasting the code from that one and then solving it with the answers below, but then it just went 'Process finished with exit code 0', and got no output when there was supposed to be lots. I then figured out that I didn't call it and then unindented the last line, but then the EOFError came again.
I have tried your code in my environment. The cause is the specification of the data reader, 'yahoo' is not needed. However, I could not get all the issues. There are some cases where the data does not exist for some stocks, so you need to have the corresponding code.
#data = web.get_data_yahoo(ticker, 'yahoo', start, end)
data = web.get_data_yahoo(ticker, start, end)