Created
March 29, 2016 16:10
-
-
Save JimmyMow/ba1bf3c22c422d09381c to your computer and use it in GitHub Desktop.
Given a ticker symbol return the name of the company. This uses Google's finance API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
import re | |
url = 'http://www.google.com/finance/info?infotype=infoquoteall&q=' | |
def get_name(ticker): | |
try: | |
response = urllib2.urlopen(url+ticker) | |
except: | |
print("Oops! Looks like we don't support that ticker.") | |
return | |
data = response.read() | |
match = re.search('"name" : ".(.*?)"', data) | |
if match: | |
name = match.group().split(':')[1].translate(None, '!@#$%^&*",.').strip() | |
print(name) | |
return | |
else: | |
print("Oops! Looks like we couldn't find a name for you.") | |
return | |
print("WMT") | |
get_name('WMT') | |
print("------------------------------------") | |
print("F") | |
get_name('F') | |
print("------------------------------------") | |
print("MMM") | |
get_name('MMM') | |
print("------------------------------------") | |
print("XXX") | |
get_name('XXX') | |
print("------------------------------------") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment