Python Forum

Full Version: Cant get financial data from google
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import bs4 as bs
import datetime as dt
import os
import pandas as pd
import pandas_datareader.data as web
import pickle
import requests

def save_ftse100_tickers():
resp = requests.get('https://en.wikipedia.org/wiki/FTSE_100_Index')
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')[1].text
tickers.append(ticker)

if ticker.find('_'):
ticker = ticker.replace('_','-')
print('trying to read modified {}'.format(ticker))

with open('ftse100tickers.pickle','wb') as f:
pickle.dump(tickers, f)

print(tickers)
return tickers
save_ftse100_tickers()

def get_data_from_google(reload_ftse100=False):
if reload_ftse100:
tickers = save_ftse100_tickers()
else:
with open('ftse100tickers.pickle','rb') as f:
tickers = pickle.load(f)

if not os.path.exists('stock_dfs'):
os.makedirs('stock_dfs')

start = dt.datetime(2016,1,4)
end = dt.datetime(2017,1,2)

for ticker in tickers:
if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
df = web.DataReader(ticker, 'google', start, end)
df.to_csv('stock_dfs/{}.csv'.format(ticker))
else:
print('Already have {}'.format(ticker))

get_data_from_google()

Traceback:
Warning (from warnings module):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas_datareader/google/daily.py", line 40
warnings.warn(UNSTABLE_WARNING, UnstableAPIWarning)
UnstableAPIWarning:
The Google Finance API has not been stable since late 2017. Requests seem
to fail at random. Failure is especially common when bulk downloading.