Extract coordinates from a embed google maps with popup in R

I'd like to catch these coordinates using R webscraping tool:

enter image description here

This is what I'm trying just coping the xpath where the coordinates are in the page but doing so xpath catch nothing.

html <- 'https://guiaempresas.universia.es/LOGISTICA-EGA.html'
x <- tryCatch(expr= {read_html(htmls)}, error=function(e){NA})
xpath <- '//*[@id="mapDiv"]/div/div/div[3]/div/div/div/div/div[1]/div[1]'
xx <- x %>% html_nodes(xpath = xpath)

This is what returns:

xx {xml_nodeset (0)}

Whats is the method to do so in R?

Upvotes: 1

Views: 483

Answers (1)

QHarr
QHarr

Reputation: 84465

You can extract the decimal values easily as follows. Then use a package like celestial to format in dms as desired.

library(magrittr)
library(rvest)
library(celestial)

page <- read_html('https://guiaempresas.universia.es/LOGISTICA-EGA.html')

lat <- page %>% html_node('.latitude .value-title') %>% html_text() %>% as.numeric()
lon <- page %>% html_node('.longitude .value-title') %>% html_text() %>% as.numeric()

# https://rdrr.io/cran/celestial/man/deg2dms.html

print(deg2dms(lat, type='cat', sep='dms', digits=1))
print(deg2dms(lon, type='cat', sep='dms', digits=1))

Upvotes: 1

Related Questions