1

I'm trying to retrieve Sector and Industry data for a list of stocks from Yahoo Finance

My code worked the first time I ran it, but now I get a long response that ends in "TypeError: string indices must be integers"

I've seen several posts that recommend using an override for pandas datareader to fix this a similar "string indices must be integers" but I'm not using pandas datareader as I'm looking to retrieve sector and industry from the profile of each ticker.

My code looks like this

import yfinance as yf

companies = ['AAPL','AMZN','GOOG','META','NFLX','TSLA']

i=0
while i<len(companies):
    stock=yf.Ticker(companies[i])
    print(stock.info['sector'])
    print(stock.info['industry'])

Response is as follows:

Traceback (most recent call last):
  File "C:/Python_Programs/stack_overflow_code.py", line 8, in <module>
    print(stock.info['sector'])
  File "C:\PerfLogs\Python Software\Python 3_10_7\lib\site-packages\yfinance\ticker.py", line 138, in info
    return self.get_info()
  File "C:\PerfLogs\Python Software\Python 3_10_7\lib\site-packages\yfinance\base.py", line 894, in get_info
    data = self._quote.info
  File "C:\PerfLogs\Python Software\Python 3_10_7\lib\site-packages\yfinance\scrapers\quote.py", line 27, in info
    self._scrape(self.proxy)
  File "C:\PerfLogs\Python Software\Python 3_10_7\lib\site-packages\yfinance\scrapers\quote.py", line 58, in _scrape
    quote_summary_store = json_data['QuoteSummaryStore']
TypeError: string indices must be integers
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
maxfactor
  • 11
  • 3
  • 1
    your `json_data` looks to be a string. How is that given a value? Perhaps you are getting a 400 response and the "text" is not json as you expect. – JonSG Jan 13 '23 at 16:54
  • ^ Agreed. If `json_data` is indeed a string, see: https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers – Maxpm Jan 13 '23 at 16:55
  • ...yeah, but the error is down in the yahoo finance package. That's particularly strange. Can't think how that could be the caller's fault. – CryptoFool Jan 21 '23 at 02:30

2 Answers2

1

Try updating the yfinance package through pip using:

pip3 install --upgrade yfinance

It worked for me.

Kostadin
  • 11
  • 1
  • Worked for me as well but still got some gunk that came with it: Yahoo has again changed data format, yfinance now unsure which key(s) is for decryption: '80226cfb77c7'-> , '80226cfb77c7'-> , '80226cfb77c7'-> , '80226cfb77c7'-> , ...... and so on for a few hundred lines. Works for most tickers though – passwortknacker Jan 24 '23 at 19:45
0

Yahoo also recently changed the layout for the .info property, so that will fail to be fetched.

See this Github Issue

Here is also an alternative way to use the library

import yfinance as yf

companies = ['AAPL','AMZN','GOOG','META','NFLX','TSLA']

tickers = yf.Tickers(' '.join(companies))

for ticker in companies:
    print(tickers.tickers)
    print(tickers.tickers[ticker].info)
    print(tickers.tickers[ticker].sector)
    print(tickers.tickers[ticker].industry)

  • 1
    Unfortunately, I got the same error running the code you provided Based on the Github link, it looks like it's a recent problem with the way Yahoo Finance encrypts and decrypts the data. If anyone comes up with a fix for this, please send it along. Thank you – maxfactor Jan 13 '23 at 22:22