Reputation: 11
I'm creating a loop to iterate a function. The function is simply getting data from yahoo finance by a list of tickers. However, some tickers do not have data in yahoo finance and sometimes there has bug, so I need to re-run the function whenever I got this error.
Basically, re-run can solve the bug, but it can't help if there has no data in the database. So, I want to use a loop defining that if there has error, then re-run, but skip that ticker if there error appears 3 times for that ticker.
I think I have done something wrong in the loop, it didn't pass that ticker and it keep re-running even it's already getting error for that ticker for more than 3 times. May I know how can I solve it?
Thanks!
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pickle
import datetime as dt
import os
import pandas as pd
import pandas_datareader.data as web
def save_hsci_tickers():
driver = webdriver.Chrome(r"C:/Users/kman/Downloads/chromedriver_win32/chromedriver.exe")
wait = WebDriverWait(driver, 10)
driver.get("https://www.hsi.com.hk/HSI-Net/HSI-Net?cmd=tab&pageId=en.indexes.hscis.hsci.constituents&expire=false&lang=en&tabs.current=en.indexes.hscis.hsci.overview_des%5Een.indexes.hscis.hsci.constituents&retry=false")
tickers = []
for name in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "table.greygeneraltxt td.greygeneraltxt,td.lightbluebg"))):
data = str(name.get_attribute('textContent'))
tickers.append(data)
edit = [x for x in tickers if x != '']
final = edit[::2]
driver.quit()
def yahoo_ticker(data):
if len(data) <= 4:
return data.zfill(4) + '.HK'
else:
return data[0:] + '.HK'
yahoo_tickers = [yahoo_ticker(data) for data in final]
with open("hscitickers.pickle","wb") as f:
pickle.dump(yahoo_tickers, f)
print(yahoo_tickers)
return yahoo_tickers
save_hsci_tickers()
def get_data_from_yahoo (reload_hscitickers=False):
if reload_hscitickers:
tickers = save_hsci_tickers()
else:
with open("hscitickers.pickle","rb") as f:
tickers = pickle.load(f)
if not os.path.exists('stock_dfs'):
os.makedirs('stock_dfs')
start = dt.datetime(2009,6,30)
end = dt.datetime(2017,6,30)
for ticker in tickers:
print(ticker)
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))
attempts = 0
while True:
try:
get_data_from_yahoo()
except:
if attempts < 3:
attempts += 1
continue
if attempts >= 3:
pass
else:
break
Upvotes: 0
Views: 276
Reputation: 175
You have to define the variable attempts outside the while loop to get it to work.
Upvotes: 1