【问题标题】:Subscript out of bounds error when scraping using an xpath with the rvest package使用带有 rvest 包的 xpath 进行抓取时,下标越界错误
【发布时间】:2021-02-01 18:40:19
【问题描述】:

我正在尝试使用 rvest 包从网站上抓取表格:

library("rvest")
uci_html <- read_html("http://archive.ics.uci.edu/ml/datasets.html")
uci_data <- uci_html %>%
  html_nodes(xpath="/html/body/table[2]/tbody/tr/td[2]/table[2]") %>%
  html_table()
uci_data <- uci_data[[1]]

就我所看到的所有示例而言,我使用的格式应该可以工作,但是 R 没有抓取任何数据,因此我收到了错误:

uci_data[[1]] 中的错误:下标超出范围

您知道为什么会出现这种情况以及我可以做些什么来抓取数据吗?

【问题讨论】:

    标签: r rvest


    【解决方案1】:

    我不太明白,但看起来tbody是不必要的。

    library("rvest")
    uci_html <- read_html("http://archive.ics.uci.edu/ml/datasets.html")
    uci_data <- uci_html %>%
       html_nodes(xpath="/html/body/table[2]/tr/td[2]/table[2]") %>% html_table( fill=TRUE)
    uci_data <- uci_data[[1]]
    

    使用html标签的另一种方式是:

    tables<-uci_html %>% html_nodes("table") 
    html_table(tables[6], fill=TRUE)[[1]]
    

    为了确定第六个表是感兴趣的表,它涉及一些试验和错误,但我发现使用 html 标记比 xpath 形式更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-17
      • 2018-10-13
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多