Webscraping Rvest not working from html page, table showing NA'S - Mc Donalds

Viewed 61

I am trying to scrape data from https://www.mcdonalds.com/de/de-de/product/grand-cheese-n-beef-classic-5642.html to make a dataframe with all the nutri values and allerges drop down menu,(Further information, per 100g, per portion, contained allergies), however my rvest cannot detect the information as a table.

I don't even show any required value

library(rvest)
url4 <- "https://www.mcdonalds.com/de/de-de/product/grand-cheese-n-beef-classic-5642.html"

test <- url4 %>% read_html() %>% 
  html_nodes(xpath = '//*[@id="collapseOne"]/div/div/div/div[1]') %>% 
  html_table()   

test <- as.data.frame(test)

I also tried this

library(rvest)
library(stringr)
library(tidyr)
url <- "https://www.mcdonalds.com/de/de-de/product/grand-cheese-n-beef-classic-5642.html"
webpage <- read_html(url)
sb_table <- html_nodes(webpage, 'table')
sb <- html_table(sb_table)[[1]]
head(sb)

How could that be done, I'm very new to web scraping don't know if it's Html tags are correct ------ This is scraping data I want--------- enter image description here link correct or not.

1 Answers

You can request the information from their json API

library(tidyverse)
library(httr2)

"https://www.mcdonalds.com/dnaapp/itemDetails?country=de&language=de&showLiveData=true&item=201799" %>%  
  request() %>%  
  req_perform() %>%  
  resp_body_json(simplifyVector = TRUE) %>%  
  .$item %>%  
  .$nutrient_facts %>%  
  .$nutrient %>%
  as_tibble %>%  
  select(4:9) 

# A tibble: 10 x 6
      id name                        nutrient_~1 uom   uom_d~2 value
   <int> <chr>                       <chr>       <chr> <chr>   <chr>
 1     1 Serving Size                primary_se~ g     grams   302  
 2     2 Brennwert                   energy_kJ   kJ    kiloJo~ 2992 
 3     3 Brennwert                   energy_kcal kcal  kilo c~ 716  
 4     4 Fett                        fat         g     grams   40   
 5     5 davon gesättigte Fettsäuren saturated_~ g     grams   16   
 6     6 Kohlenhydrate               carbohydra~ g     grams   44   
 7     7 davon Zucker                sugar       g     grams   11   
 8     8 Ballaststoffe               fiber       g     grams   3.3  
 9     9 Eiweiß                      protein     g     grams   40   
10    10 Salz                        salt        g     grams   2.4  
# ... with abbreviated variable names 1: nutrient_name_id,
#   2: uom_description

Information on the allergies

"https://www.mcdonalds.com/dnaapp/itemDetails?country=de&language=de&showLiveData=true&item=201799" %>%
  request() %>%
  req_perform() %>%
  resp_body_json(simplifyVector = TRUE) %>% 
  .$item %>% 
  .$item_allergen %>% 
  str_split(pattern = ",  ") %>% 
  getElement(1)

[1] "Milch (einschl. Laktose)"                                         
[2] "Eier"                                                             
[3] "Glutenhaltiges Getreide:  Weizen (wie Dinkel und Khorasan-Weizen)"
[4] "Senf"                                                             
[5] "Sesamsamen"   
Related