Retrieve company name with ticker symbol input, yahoo or google API
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)
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
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')