Reputation: 11
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
Upvotes: 1
Views: 1381
Reputation: 11
Try updating the yfinance package through pip using:
pip3 install --upgrade yfinance
It worked for me.
Upvotes: 1
Reputation: 276
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)
Upvotes: 0