Search code examples
pythonpython-requestsyahoo-finance

Yahoo Finance API. Get company name from stock symbol without loading the entire YFinance module


I would like to use either the python request module or the urllib module to get the company name by passing the stock symbol to the Yahoo Finance API. I don't want to use the YFinance Module because I don't need Panda's, Numpy, etc. I am trying to keep the size of my program down.

Here is the endpoint from the YFinance wrapper module:

name = yf.Ticker(symbol).info['shortName']

I'm just not sure how to make the request without the module.


Solution

  • Using the endpoint from @r-beginners comment, I created a simple function using the urllib module to retrieve the company name from Yahoo Finance in Python. No YFinance module is needed. I hope this is helpful to someone else.

    def get_yahoo_shortname(symbol):
        response = urllib.request.urlopen(f'https://query2.finance.yahoo.com/v1/finance/search?q={symbol}')
        content = response.read()
        data = json.loads(content.decode('utf8'))['quotes'][0]['shortname']
        return data