You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am looping over a long list of tickers to scrape data and every so often, the shares method returns a None type.
If I catch that and keep trying to run the method later in time (a minute, 2 minutes even), it keeps returning the None type over and over. If I interrupt manually and restart the script instead, odds are very high to getting a dataframe with the share info returned.
Here's part of my code:
import yfinance as yf
from pathlib import Path
import csv
import time
import json
from datetime import datetime
def make_path(ticker):
subfolder = f'data/{ticker}'
Path(subfolder).mkdir(parents=True, exist_ok=True)
return subfolder
def get_shares(stock):
return stock.shares
def safe_shares_data(ticker, stock):
folder = make_path(ticker)
time.sleep(0.5)
shares = get_shares(stock)
while shares is None:
log(ticker, 'Trying to get share data again')
time.sleep(20)
shares = get_shares(stock)
shares.to_csv(path_or_buf=f'{folder}/shares_out_{ticker}.csv')
def log(ticker, msg):
now = datetime.now()
message = f'{now.strftime("%Y-%m-%d %H:%M:%S")}: {ticker} - {msg}\n'
with open('log.txt', 'a') as flog:
flog.write(message)
tickers = ['AAPL', 'V', 'MA', 'HEI'] #EXAMPLE. tickers is a long list with thousands of stocks in alphabetical order
for ticker in tickers:
time.sleep(1)
stock = initialize_yf(ticker)
info = stock.info
print("Length of info:", len(info))
if len(info) < 10: # test for delisting.
continue
sector = info.get('sector') # exclude 'Financial Services'
time.sleep(0.5)
if sector != "Financial Services":
safe_shares_data(ticker, stock)
I have no idea what to make of it and why that happens. The shares method works well manually so I doubt that it's a bug. Is it that yahoo is kicking me off? But if that's the case, why can I re-take the script if i start it again?
The text was updated successfully, but these errors were encountered:
The solution to not get blocked by spam detection is to stop spamming. shares changes once a year, why can't you cache it somewhere? requests session is a basic dumb way to cache, works for most (all?) financial data, it's documented in the README.
Hi
I am looping over a long list of tickers to scrape data and every so often, the shares method returns a None type.
If I catch that and keep trying to run the method later in time (a minute, 2 minutes even), it keeps returning the None type over and over. If I interrupt manually and restart the script instead, odds are very high to getting a dataframe with the share info returned.
Here's part of my code:
I have no idea what to make of it and why that happens. The shares method works well manually so I doubt that it's a bug. Is it that yahoo is kicking me off? But if that's the case, why can I re-take the script if i start it again?
The text was updated successfully, but these errors were encountered: