Search code examples
javascriptrweb-scrapingscreen-scrapingrvest

Fetch JS nested element in HTML page with Rvest


I'm attempting to retrieve elements on this page with Rvest to put them in a dataframe.

There's a bit of text seemingly nested in a JS element that I want in my df though I struggle to scrape that very bit.

Here's the code of the page where is the element :

<div class="js-unregulated-speciality-alert" data-props="{}">
  <div class="dl-alert dl-alert-unregulated-speciality dl-margin-t dl-alert-info dl-alert-size-medium">
    <div class="dl-alert-content">
        <svg class="dl-icon dl-margin-r-s dl-icon-small" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
            <path fill-rule="evenodd" clip-rule="evenodd" d="M8 14.4A6.4 6.4 0 108 1.6a6.4 6.4 0 000 12.8zm0-7.793a1.084 1.084 0 110-2.168 1.084 1.084 0 010 2.168zm1.135 4.696h-2.27a.31.31 0 01-.31-.31v-.619a.31.31 0 01.31-.31h.31v-1.65h-.31a.31.31 0 01-.31-.31v-.619a.31.31 0 01.31-.31h1.651a.31.31 0 01.31.31v2.58h.31a.31.31 0 01.31.31v.62a.31.31 0 01-.31.31z"></path></svg>
                <span class="dl-text dl-text-body dl-text-regular dl-text-s">
                    Ce praticien exerce une profession non réglementée.
            <span class="dl-underlined dl-cursor-pointer dl-font-700 dl-margin-l-xs">En savoir plus</span>
            </span>
    </div>
</div>

The bit "Ce praticien exerce une profession non réglementée." is exactly what I need to be scraped. This R code does only return an empty character.

link <- "https://www.doctolib.fr/hypnotherapeute/paris/elsa-couteiller"        
page = read_html(link)
text = page %>%
   html_nodes("js-unregulated-speciality-alert") %>%
   html_text()

Solution

  • Data is dynamically loaded from a script tag. You can use the following regex to extract the profile info then parse as json and pull out your target phrase:

    library(rvest)
    library(stringr)
    library(dplyr)
    library(jsonlite)
    
    page <- read_html('https://www.doctolib.fr/hypnotherapeute/paris/elsa-couteiller') %>% toString()
    res <- page %>% stringr::str_match("window.translation_keys = (.*\\});\\n") %>% .[2]
    data <- jsonlite::parse_json(res)
    print(data$root$profiles$show$unregulated_speciality)