[英]Subscript out of bounds error when scraping using an xpath with the rvest package
我正在嘗試使用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]] 中的錯誤:下標越界
你知道為什么會這樣嗎,我可以做些什么來抓取數據?
我不太明白,但看起來 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 表單更容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.