yfinance中的“ KeyError”-PYTHON

I am trying to analyze stocks within python using the api "yfinance." I get the program to run just fine most of the time. However, when it is unable to 'find' one of the fields, it throughs up an error (KeyError). Here is a snippet of my code. import yfinance as yf stock = yf.Ticker(stock) pegRatio = stock.info['pegRatio'] print(pegRatio) if pegRatio > 1: print("The PEG Ratio for "+name+" is high, indicating that it may be overvalued (based on projected earnings growth).") if pegRatio == 1: print("The PEG Ratio for "+name+" is 1, indicating that it is close to fair value.") if pegRatio < 1: print("The PEG Ratio for "+name+" is low, indicating that it may be undervalued (based on projected earnings growth).") My main question is how do I get the code to basically ignore the error and run the rest of the code?

评论