paulz paulz - 1 month ago 26
JSON Question

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"

Answer

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)