带有 rvest 的 xpath 失败,返回一个空列表

xpath with rvest fails, returning an empty list

我在使用 "rvest" 从页面提取 COVID 数据时卡住了:https://www.cdc.gov/coronavirus-interactive/index.html

感谢您的宝贵时间!

这是我的代码:

library("rvest")

url = "https://www.cdc.gov/coronavirus-interactive/index.html"

tbl <- url %>%
  read_html() %>%
  html_nodes(xpath = '//*[@id="viz030_widget5_table"]') %>%
  html_table(fill=TRUE)
tbl

它是动态检索的。您可以从

检索 json 格式的所需数据

https://www.cdc.gov/covid-data-tracker/Content/CoronaViewJson_01/US_MAP_DATA.json

您可以在刷新原始页面时在网络选项卡中找到它。

library(jsonlite)

data <- jsonlite::read_json('https://www.cdc.gov/covid-data-tracker/Content/CoronaViewJson_01/US_MAP_DATA.json') %>% 
        .$US_MAP_DATA