我想通过下载此页面上的所有CSV文件(http://cfe.cboe.com/Products/historicalVIX.aspx)获取VIX期货的历史价格。下面是我使用这样做代码:从CBOE下载VIX期货价格
library(XML)
#Extract all links for url
url <- "http://cfe.cboe.com/Products/historicalVIX.aspx"
doc <- htmlParse(url)
links <- xpathSApply(doc, "//a/@href")
free(doc)
#Filter out URLs ending with csv and complete the link.
links <- links[substr(links, nchar(links) - 2, nchar(links)) == "csv"]
links <- paste("http://cfe.cboe.com", links, sep="")
#Peform read.csv on each url in links, skipping the first two URLs as they are not relevant.
c <- lapply(links[-(1:2)], read.csv, header = TRUE)
我得到的错误:
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
more columns than column names
经进一步调查,我意识到这是因为有些CSV文件的格式不同。如果我手动加载URL links[9]
,我看到第一排有此免责声明:
CFE data is compiled for the .......use of CFE data is subject to the Terms and Conditions of CBOE's Websites.
大多数其他文件(例如links[8]
和links[10]
)的都很好,因此这似乎已随机插入。是否有一些R魔法可以解决这个问题?
谢谢。
谢谢。我不需要重新发明轮子。 – mchangun
感谢您指出VX_N13在第一行有免责声明。我会尽力修补,以尽快处理。 – GSee
@GSee有没有像getQuote.cfe可用的东西。我环顾四周,无法找到它。我在问,因为使用getSmbols.cfe提供的数据有点晚了“请注意:CFE数据直到上午10:00左右C.T.在下一个工作日才可用。”问题在于何处获得(接近)实时数据。一个明显的地方是盈透证券......任何其他想法(谷歌,雅虎,...)?日Thnx。 – Samo