问题
Some sort of free REST API would be ideal, but in general is there any free API or web service or CSV file (that's not behind a password prompt) or anything out there that one can query to get the current list of the S&P 500 index constituents?
I've looked on S&P's site itself (http://www.standardandpoors.com), through Yahoo Finance's API, and Markit on demand (http://dev.markitondemand.com/), but have not been able to find anything yet.
回答1:
I found http://finviz.com/export.ashx?v=152&f=idx_sp500&ft=1&ta=1&p=d&r=1&c=1
:-)
But I haven't found Finviz API documentation.
:-(
Bloomberg seems to have an open api. Might find the data you need if you dig around.
回答2:
Also had a similar need. You could use Wikipedia API or parse html to get the list of symbols in S&P 500 http://en.wikipedia.org/wiki/List_of_S%26P_500_companies
You can now install and use module by
pip install finsymbols
I currently obtain the list of symbols via Wikipedia. It is not rest but can be easily made into a rest API. It is written in python
>>import sys
>>sys.path.append('/home/skillachie/Desktop/')
>>import finsymbols
sp500 = finsymbols.get_sp500_symbols()
pprint.pprint(sp500)
{'company': u'Xcel Energy Inc',
'headquaters': u'Minneapolis, Minnesota',
'industry': u'Multi-Utilities & Unregulated Power',
'sector': u'Utilities',
'symbol': u'XEL'},
{'company': u'Xerox Corp.',
'headquaters': u'Norwalk, Connecticut',
'industry': u'IT Consulting & Services',
'sector': u'Information Technology',
'symbol': u'XRX'},
{'company': u'Xilinx Inc',
'headquaters': u'San Jose, California',
'industry': u'Semiconductors',
'sector': u'Information Technology',
'symbol': u'XLNX'},
{'company': u'XL Capital',
'headquaters': u'Hamilton, Bermuda',
'industry': u'Property & Casualty Insurance',
'sector': u'Financials',
'symbol': u'XL'},
If interested you can get more information here http://skillachie.github.io/finsymbols/
回答3:
with python, you can try out this snippet i just wrote (a bit ugly, but that works). (it actually returns 502 tickers. and it is right)
from urllib import request
from bs4 import BeautifulSoup
import datetime
import dateutil.relativedelta as dr
import pandas as pd
def get_constituents():
# URL request, URL opener, read content
req = request.Request('http://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
opener = request.urlopen(req)
content = opener.read().decode() # Convert bytes to UTF-8
soup = BeautifulSoup(content)
tables = soup.find_all('table') # HTML table we actually need is tables[0]
external_class = tables[0].findAll('a', {'class':'external text'})
tickers = []
for ext in external_class:
if not 'reports' in ext:
tickers.append(ext.string)
return tickers
来源:https://stackoverflow.com/questions/13769278/are-there-any-free-apis-for-retrieving-the-sp-500s-component-symbols