使用R的htmltab时出错:`* tmp *`[[index]]中的错误:下标超出范围

时间:2017-02-03 03:03:02

标签: r finance google-finance

尝试使用

从Google财经下载INX
x <- htmltab(doc = "https://www.google.com/finance/historical?q=INDEXSP%3A.INX&ei=Qu-TWOn-AtW1mQGQ06WYCQ") 

并且它给出了这个错误:

Error in `*tmp*`[[index]] : subscript out of bounds

1 个答案:

答案 0 :(得分:0)

我无法使htmltab工作,但您可以使用library(rvest)解析网页,并指定表格的特定xpath

library(rvest)

url <- "https://www.google.com/finance/historical?q=INDEXSP%3A.INX&ei=Qu-TWOn-AtW1mQGQ06WYCQ"

read_html(url) %>%
    html_node(xpath = "//*[@class='gf-table historical_price']") %>%
    html_table()

#            Date     Open     High      Low    Close        Volume
# 1   Feb 2, 2017 2,276.69 2,283.97 2,271.65 2,280.85 2,321,960,100
# 2   Feb 1, 2017 2,285.59 2,289.14 2,272.44 2,279.55 2,478,979,663
# 3  Jan 31, 2017 2,274.02 2,279.09 2,267.21 2,278.87 2,555,320,206
# 4  Jan 30, 2017 2,286.01 2,286.01 2,268.04 2,280.90 2,108,083,825
# ...