从长度可变的列表中刮取最后一个项目

时间:2019-11-14 13:56:41

标签: r rvest

我从年度排名中抓取经济数据-总是抓取第一个值和最后一个值。现在的问题是,列表的长度每年都在变化,因此最后一个节点总是不同的。

Year Last list item
1997 89
1998 94
1999 112
2000 76

以此类推

是否有可能自动到达最后一个节点?

page_call <- read_html("https://...")
input <- page_call %>%
  html_nodes(xpath="/html/body/div/div[Number of last node]") %>%
  html_text() 

谢谢!

1 个答案:

答案 0 :(得分:1)

last()应该起作用:

page_call <- read_html("https://...")
input <- page_call %>%
  html_nodes(xpath="/html/body/div/div[last()]") %>%
  html_text()