How to extract this node content using rvest

Viewed 85

I'm scraping this website. I'm specifically interested in extracting the content found in the last script node script node snippet. Thus far I have tried the following:

url <- "https://insolvencyinsider.ca/filing/"
ii <- read__html(url)
fwp <- ii %>%
  htl_nodes("body") %>%
  xml_find_first(xpath = "/script[15]") %>%
  html_text() # Not text so I wouldn't expect this to work.

#> character (empty)


fwp <- ii %>%
  htl_nodes("body") %>%
  xml_find_first(xpath = "/script[15]") %>%
  html_attr("window.FWP_JSON") # Don't think this makes sense since its not an attribute?

 #> chr NA
1 Answers

You can regex it out with the following pattern then parse with jsonlite

library(rvest)
library(jsonlite)
library(stringr)

text <- read_html('https://insolvencyinsider.ca/filing/') %>% 
  toString()

data <- stringr::str_match(text, 'window\\.FWP_JSON = (.*?);\\n')[,2]

result <- jsonlite::parse_json(data)

Regex:

enter image description here

Related