Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve company name with ticker symbol input, yahoo or google API

Just looking for a simple api return, where I can input a ticker symbol and receive the full company name:

ticker('MSFT') will return "Microsoft"

like image 760
paulz Avatar asked Aug 16 '16 05:08

paulz


3 Answers


import yfinance as yf

msft = yf.Ticker("MSFT")

company_name = msft.info['longName']

#Output = 'Microsoft Corporation'

So this way you would be able to get the full names of companies from stock symbols

like image 147
Tejas Krishna Reddy Avatar answered Nov 08 '22 01:11

Tejas Krishna Reddy


You need to first find a website / API which allows you to lookup stock symbols and provide information. Then you can query that API for information.

I came up with a quick and dirty solution here:

import requests


def get_symbol(symbol):
    symbol_list = requests.get("http://chstocksearch.herokuapp.com/api/{}".format(symbol)).json()

    for x in symbol_list:
        if x['symbol'] == symbol:
            return x['company']


company = get_symbol("MSFT")

print(company)

This website only provides company name. I didn't put any error checks. And you need the requests module for it to work. Please install it using pip install requests.

Update: Here's the code sample using Yahoo! Finance API:

import requests


def get_symbol(symbol):
    url = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={}&region=1&lang=en".format(symbol)

    result = requests.get(url).json()

    for x in result['ResultSet']['Result']:
        if x['symbol'] == symbol:
            return x['name']


company = get_symbol("MSFT")

print(company)
like image 14
masnun Avatar answered Nov 08 '22 00:11

masnun


Using fuzzy match to get company symbol from company name or vice versa

from fuzzywuzzy import process
import requests

def getCompany(text):
    r = requests.get('https://api.iextrading.com/1.0/ref-data/symbols')
    stockList = r.json()
    return process.extractOne(text, stockList)[0]


getCompany('GOOG')
getCompany('Alphabet')
like image 4
Rajan Mehta Avatar answered Nov 08 '22 01:11

Rajan Mehta