Search code examples
htmlrnodesrvestquote

web-scraping: how to include quote character in HTML node


I am using the rvest package to scrape information from a website. Some information that I need belong to the class iinfo". Unfortunately, if I use this string inside the function html_nodes() I got the following error:

Error in parse_simple_selector(stream) : 
  Expected selector, got <STRING '' at 7>

Here's a reprex:

library(rvest)
library(xml2)

webpage <- read_html(x = paste0("https://www.gstsvs.ch/fr/trouver-un-medecin-veterinaire.html?tx_datapool_pi1%5Bhauptgebiet%5D=3&tx_datapool_pi1%5Bmapsearch%5D=cercare&tx_datapool_pi1%5BdoSearch%5D=1&tx_datapool_pi1%5Bpointer2303%5D=",
                                  0))

webpage_address <- webpage %>%
    html_nodes('.iinfo"') %>%
    html_text() %>%
    gsub(pattern = "\r|\t|\n",
         replacement = " ")

That class refers to the addresses listed inside every box of the website. You can retrieve this information if, in the browser, you inspect the webpage structure and navigate to that box. If you do so, when you select the address division with the mouse, you'll see that a flag with div.iinfo\" appears.

Thanks a lot for your help!


Solution

  • Here:

    webpage_address <- webpage %>%
      html_nodes(xpath = "//*[@class='iinfo\"']") %>%
      html_text(trim = T) 
    

    Result:

    > webpage_address
     [1] "Anne-Françoise HenchozEnvers 412400 Le Locle, NE"                                                              
     [2] "Téléphone: 032 931 10 10Urgences: 032 931 10 10Fax: 032 931 36 10afhenchoz(at)bluewin.chafhenchoz.com"         
     [3] "Ursi Dommann ScheuberHauptstrasse 156222 Gunzwil, LU"                                                          
     [4] "Téléphone: 041 930 14 44tiergesundheit(at)bluewin.ch"                                                          
     [5] "Dr. Med. Vet. Anne KramerBaggwilgraben 33267 Seedorf, BE"                                                      
     [6] "Téléphone: 079 154 70 15anne(at)alpakavet.chwww.alpakavet.ch"                                                  
     [7] "Dr. med. vet. Andrea FeistAdelbodenstrasse 103714 Frutigen, BE"                                                
     [8] "Téléphone: 033 671 15 60Urgences: 033 671 15 60Fax: 033 671 86 60alpinvet(at)bluewin.chwww.alpinvet.ch"        
     [9] "Dr. med. vet. Peter KürsteinerAlpsteinstr. 289240 Uzwil, SG"                                                   
    [10] "Téléphone: 071 951 85 44"                                                                                      
    [11] "Kathrin Urscheler-Hollenstein, Eveline Muhl-ZollingerSchaffhauserstrasse 2458222 Beringen, SH"                 
    [12] "Téléphone: 052 685 20 20Fax: 052 685 34 20praxis(at)tieraerzte-team.chwww.tieraerzte-team.ch"                  
    [13] "Dr. med. vet. Erwin VincenzVia Santeri 127130  Ilanz, GR"                                                      
    [14] "Téléphone: 081/925 23 23Urgences: 081/925 23 23Fax: 081/925 23 25info(at)anima-veterinari.ch"                  
    [15] "Dr. Zlatko MarinovicMühlerain 3853855072 oeschgen, AG"                                                         
    [16] "Téléphone: 49628715060Urgences: 49628715060Fax: 49628712439z.marin(at)sunrise.ch"                              
    [17] "Manser ChläusSchwalbenweg 73186 Düdingen, FR"                                                                  
    [18] "Téléphone: 026 493 10 60animans.tierarzt(at)gmail.com"                                                         
    [19] "W.A.GeesBrünigstrasse 38aHauptstrasse 100, 3855 Brienz3860 Meiringen, BE"                                      
    [20] "Téléphone: 033 / 971 60 42Urgences: 033 / 971 60 42Fax: 033 / 971 01 50info(at)tierarzt-meiringen.chanisano.ch"