Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading