如何使用R获得所有S& P500公司代码清单?

时间:2017-06-29 06:50:28

标签: r

我想在R中使用FOR语句一次性分析股票数据。

但我不知道如何获得S& P公司代码清单。

请让我知道解决方案。

谢谢。

ul li

2 个答案:

答案 0 :(得分:1)

如果您使用彭博/路透社或任何其他数据提供商,那么您可以在Excel中获得您想要的任何内容,然后将其读取到R,然后继续下载历史数据。如果您无法访问此类商业数据提供商,那么其中一种方式是网络抓取(我相信维基百科足够可靠,但需要仔细检查):

library(rvest)
url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
SP500 <- url %>%
  html() %>%
  html_nodes(xpath='//*[@id="mw-content-text"]/div/table[1]') %>%
  html_table()
SP500 <- SP500[[1]]
Tix <- SP500$`Ticker symbol`
Tix

截至今天,S&amp; P500的成分:

> Tix
  [1] "MMM"   "ABT"   "ABBV"  "ACN"   "ATVI"  "AYI"   "ADBE"  "AMD"   "AAP"  
 [10] "AES"   "AET"   "AMG"   "AFL"   "A"     "APD"   "AKAM"  "ALK"   "ALB"  
 [19] "ARE"   "ALXN"  "ALGN"  "ALLE"  "AGN"   "ADS"   "LNT"   "ALL"   "GOOGL"
 [28] "GOOG"  "MO"    "AMZN"  "AEE"   "AAL"   "AEP"   "AXP"   "AIG"   "AMT"  
 [37] "AWK"   "AMP"   "ABC"   "AME"   "AMGN"  "APH"   "APC"   "ADI"   "ANSS" 
 [46] "ANTM"  "AON"   "APA"   "AIV"   "AAPL"  "AMAT"  "ADM"   "ARNC"  "AJG"  
 [55] "AIZ"   "T"     "ADSK"  "ADP"   "AN"    "AZO"   "AVB"   "AVY"   "BHI"  
 [64] "BLL"   "BAC"   "BK"    "BCR"   "BAX"   "BBT"   "BDX"   "BBBY"  "BRK.B"
 [73] "BBY"   "BIIB"  "BLK"   "HRB"   "BA"    "BWA"   "BXP"   "BSX"   "BMY"  
 [82] "AVGO"  "BF.B"  "CHRW"  "CA"    "COG"   "CPB"   "COF"   "CAH"   "CBOE" 
 [91] "KMX"   "CCL"   "CAT"   "CBG"   "CBS"   "CELG"  "CNC"   "CNP"   "CTL"  
[100] "CERN"  "CF"    "SCHW"  "CHTR"  "CHK"   "CVX"   "CMG"   "CB"    "CHD"  
[109] "CI"    "XEC"   "CINF"  "CTAS"  "CSCO"  "C"     "CFG"   "CTXS"  "CLX"  
[118] "CME"   "CMS"   "COH"   "KO"    "CTSH"  "CL"    "CMCSA" "CMA"   "CAG"  
[127] "CXO"   "COP"   "ED"    "STZ"   "COO"   "GLW"   "COST"  "COTY"  "CCI"  
[136] "CSRA"  "CSX"   "CMI"   "CVS"   "DHI"   "DHR"   "DRI"   "DVA"   "DE"   
[145] "DLPH"  "DAL"   "XRAY"  "DVN"   "DLR"   "DFS"   "DISCA" "DISCK" "DISH" 
[154] "DG"    "DLTR"  "D"     "DOV"   "DOW"   "DPS"   "DTE"   "DD"    "DUK"  
[163] "DXC"   "ETFC"  "EMN"   "ETN"   "EBAY"  "ECL"   "EIX"   "EW"    "EA"   
[172] "EMR"   "ETR"   "EVHC"  "EOG"   "EQT"   "EFX"   "EQIX"  "EQR"   "ESS"  
[181] "EL"    "ES"    "RE"    "EXC"   "EXPE"  "EXPD"  "ESRX"  "EXR"   "XOM"  
[190] "FFIV"  "FB"    "FAST"  "FRT"   "FDX"   "FIS"   "FITB"  "FE"    "FISV" 
[199] "FLIR"  "FLS"   "FLR"   "FMC"   "FL"    "F"     "FTV"   "FBHS"  "BEN"  
[208] "FCX"   "GPS"   "GRMN"  "IT"    "GD"    "GE"    "GGP"   "GIS"   "GM"   
[217] "GPC"   "GILD"  "GPN"   "GS"    "GT"    "GWW"   "HAL"   "HBI"   "HOG"  
[226] "HRS"   "HIG"   "HAS"   "HCA"   "HCP"   "HP"    "HSIC"  "HSY"   "HES"  
[235] "HPE"   "HLT"   "HOLX"  "HD"    "HON"   "HRL"   "HST"   "HPQ"   "HUM"  
[244] "HBAN"  "IDXX"  "INFO"  "ITW"   "ILMN"  "IR"    "INTC"  "ICE"   "IBM"  
[253] "INCY"  "IP"    "IPG"   "IFF"   "INTU"  "ISRG"  "IVZ"   "IRM"   "JEC"  
[262] "JBHT"  "SJM"   "JNJ"   "JCI"   "JPM"   "JNPR"  "KSU"   "K"     "KEY"  
[271] "KMB"   "KIM"   "KMI"   "KLAC"  "KSS"   "KHC"   "KR"    "LB"    "LLL"  
[280] "LH"    "LRCX"  "LEG"   "LEN"   "LVLT"  "LUK"   "LLY"   "LNC"   "LKQ"  
[289] "LMT"   "L"     "LOW"   "LYB"   "MTB"   "MAC"   "M"     "MNK"   "MRO"  
[298] "MPC"   "MAR"   "MMC"   "MLM"   "MAS"   "MA"    "MAT"   "MKC"   "MCD"  
[307] "MCK"   "MDT"   "MRK"   "MET"   "MTD"   "KORS"  "MCHP"  "MU"    "MSFT" 
[316] "MAA"   "MHK"   "TAP"   "MDLZ"  "MON"   "MNST"  "MCO"   "MS"    "MOS"  
[325] "MSI"   "MUR"   "MYL"   "NDAQ"  "NOV"   "NAVI"  "NTAP"  "NFLX"  "NWL"  
[334] "NFX"   "NEM"   "NWSA"  "NWS"   "NEE"   "NLSN"  "NKE"   "NI"    "NBL"  
[343] "JWN"   "NSC"   "NTRS"  "NOC"   "NRG"   "NUE"   "NVDA"  "ORLY"  "OXY"  
[352] "OMC"   "OKE"   "ORCL"  "PCAR"  "PH"    "PDCO"  "PAYX"  "PYPL"  "PNR"  
[361] "PBCT"  "PEP"   "PKI"   "PRGO"  "PFE"   "PCG"   "PM"    "PSX"   "PNW"  
[370] "PXD"   "PNC"   "RL"    "PPG"   "PPL"   "PX"    "PCLN"  "PFG"   "PG"   
[379] "PGR"   "PLD"   "PRU"   "PEG"   "PSA"   "PHM"   "PVH"   "QRVO"  "PWR"  
[388] "QCOM"  "DGX"   "RRC"   "RJF"   "RTN"   "O"     "RHT"   "REG"   "REGN" 
[397] "RF"    "RSG"   "RAI"   "RHI"   "ROK"   "COL"   "ROP"   "ROST"  "RCL"  
[406] "CRM"   "SCG"   "SLB"   "SNI"   "STX"   "SEE"   "SRE"   "SHW"   "SIG"  
[415] "SPG"   "SWKS"  "SLG"   "SNA"   "SO"    "LUV"   "SPGI"  "SWK"   "SPLS" 
[424] "SBUX"  "STT"   "SRCL"  "SYK"   "STI"   "SYMC"  "SYF"   "SNPS"  "SYY"  
[433] "TROW"  "TGT"   "TEL"   "FTI"   "TSO"   "TXN"   "TXT"   "TMO"   "TIF"  
[442] "TWX"   "TJX"   "TMK"   "TSS"   "TSCO"  "TDG"   "RIG"   "TRV"   "TRIP" 
[451] "FOXA"  "FOX"   "TSN"   "UDR"   "ULTA"  "USB"   "UA"    "UAA"   "UNP"  
[460] "UAL"   "UNH"   "UPS"   "URI"   "UTX"   "UHS"   "UNM"   "VFC"   "VLO"  
[469] "VAR"   "VTR"   "VRSN"  "VRSK"  "VZ"    "VRTX"  "VIAB"  "V"     "VNO"  
[478] "VMC"   "WMT"   "WBA"   "DIS"   "WM"    "WAT"   "WEC"   "WFC"   "HCN"  
[487] "WDC"   "WU"    "WRK"   "WY"    "WHR"   "WFM"   "WMB"   "WLTW"  "WYN"  
[496] "WYNN"  "XEL"   "XRX"   "XLNX"  "XL"    "XYL"   "YUM"   "ZBH"   "ZION" 
[505] "ZTS" 

答案 1 :(得分:1)

您可以使用BatchGetSymbols软件包,该软件包还会通过抓取Wikipedia来获取列表。

library(BatchGetSymbols)

sp500 <- GetSP500Stocks()
sp500$tickers