pythonyfinance

How to suppress Output Message from Python yfinance package?


I am trying to get stock ratings using the yfinance python package with the following line:

recommendations = yf.Ticker("ANSCU").upgrades_downgrades

This works well until I run into some tickers that has no ratings (such as ANSCU). In such case I get a regular message that is intended to be an error message:

404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/ANSCU?modules=upgradeDowngradeHistory&corsDomain=finance.yahoo.com&form

This is ok and I can move on to next ticker. However, there is no option to suppress this ulgy looking 404 message. I tried redirecting system output:

print("A")
text_trap = io.StringIO()
sys.stdout = text_trap
print("B")
recommendations = yf.Ticker("ANSCU").upgrades_downgrades
sys.stdout = sys.__stdout__
print("C")

It does work on my own function's output but it does not work from within yfinance package codings as follows:

A
404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/ANSCU?modules=upgradeDowngradeHistory&corsDomain=finance.yahoo.com&formatted=false&symbol=ANSCU&crumb=tpwxFnmHnDw
C

Wonder if there is anyway to suppress the "404..." message. Thanks.


Solution

  • I believe that is a log message, not a simple print statement.

    Here is one way to disable the log messages:

    import logging
    logger = logging.getLogger('yfinance')
    logger.disabled = True
    logger.propagate = False