Search code examples
htmlrweb-scrapingxml2

Problems to access certain category using xpath in Rstudio


how are you? I am trying to access to a particular sportbetting web, i want to get the names of the football matches that are being played, but when i try i only get access to all the event names and i do not know why. I am using this code:

library(rvest)
library(tidyverse)
library(xml2)

html="https://www.supermatch.com.uy/live_recargar_menu/"

a=rvest::read_html(html)

b2= a %>% html_node("body") %>% html_node(xpath="//li[@class='sport code_sport-1']") %>% 
    html_nodes(xpath="//span[@class='titulo']") %>% html_text() 

As you can see, this code gets the name for all the events that are being played.


Solution

  • library(tidyverse)
    library(rvest)
    
    page <- "https://www.supermatch.com.uy/live_recargar_menu/" %>%
      read_html()
    
    tibble(
      title = page %>%
        html_elements(".titulo") %>%
        html_text(),
      score = page %>%
        html_elements(".marcador") %>%
        html_text(), 
      time = page %>%
        html_elements(".marcador+ span") %>%
        html_text() %>% 
        str_squish()
    ) 
    
    # A tibble: 15 × 3
       title                                      score time         
       <chr>                                      <chr> <chr>        
     1 CS Emelec - Atletico Mineiro               0:1   1ª parte 18' 
     2 Nacional de Montevideo - Union de Santa Fe 1:0   1ª parte 18' 
     3 Brusque FC - Esporte Clube Bahia           0:1   PT 34'       
     4 Gremio FBPA - Londrina                     1:0   PT 34'       
     5 Fulgencio Yegros - Deportivo Santani       2:0   2ª parte 76' 
     6 Paraguay - Panama                          2:0   2ª parte 65' 
     7 Venezuela U20 - Bolivia U20                26:10 2do cuarto 6'
     8 Liu, L - Parikh, H                         1:1   3er set      
     9 Truwit, Teddy - Raab, J                    0:0   1er Set      
    10 Ngounoue, M - Woog, M                      1:0   2do set      
    11 Cheng, E - Lopez, Jackeline                1:0   2do set      
    12 Moore, M - Pratt, S                        0:1   2do set      
    13 Zhu, Jiayun - Horwood, S                   0:0   1er Set      
    14 Nguyen, M - White, M                       0:0   1er Set      
    15 Martincova, T - Pliskova, Karolina         0:0   No iniciado