我想通過下載此頁面上的所有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