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"
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
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={}®ion=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)
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With