Home > Blockchain >  How to scrape a table created using datawrapper using rvest?
How to scrape a table created using datawrapper using rvest?

Time:01-28

I am trying to scrape Table 1 from the following website using rvest:

https://www.kff.org/coronavirus-covid-19/issue-brief/u-s-international-covid-19-vaccine-donations-tracker/

Following is the code i have written:

link <- "https://www.kff.org/coronavirus-covid-19/issue-brief/u-s-international-covid-19-vaccine-donations-tracker/"

page <- read_html(link)

page %>%   html_nodes("iframe") %>% html_attr("src") %>% .[11] %>% read_html() %>% 
  html_nodes("table.medium datawrapper-g2oKP-6idse1 svelte-1vspmnh resortable")

But, i get {xml_nodeset (0)} as the result. I am struggling to figure out the correct tag to select in html_nodes() from the datawrapper page to extract Table 1.

I will be really grateful if someone can point out the mistake i am making, or suggest a solution to scrape this table.

Many thanks.

CodePudding user response:

The data is present in the iframe but needs a little manipulation. It is easier, for me at least, to construct the csv download url from the iframe page then request that csv

library(rvest)
library(magrittr)
library(vroom)
library(stringr)

page <- read_html('https://www.kff.org/coronavirus-covid-19/issue-brief/u-s-international-covid-19-vaccine-donations-tracker/')

iframe <- page %>% html_element('iframe[title^="Table 1"]') %>% html_attr('src')


id <- read_html(iframe) %>% html_element('meta') %>% html_attr('content') %>% str_match('/(\\d )/') %>% .[, 2]

csv_url <- paste(iframe,id, 'dataset.csv', sep = '/' )

data <- vroom(csv_url, show_col_types = FALSE)
  •  Tags:  
  • Related