I am trying to scrape the marker coordinates from a website containing a leaflet
map (osm data). I have been trawling the web for answers and it appears that a simple query to the parsed html will not be sufficient, due to the dynamic nature of the website. Hence I've been using RSelenium
. After looking into the html and playing with ChatGPT, I've gotten this far:
library(RSelenium)
library(rvest)
library(xml2)
remote_driver <- rsDriver(browser = "firefox",
chromever = NULL,
verbose = FALSE,
port = 4445L)
remDr <- remote_driver$client
remDr$navigate("https://www.hejfish.com/d/1356-strobl-wasser-fliegenstrecke-traun")
# Scroll to the end of the page to trigger marker loading
remDr$executeScript("window.scrollTo(0, document.body.scrollHeight);")
map_markers <- remDr$findElements(using = "css", value = ".MapIcons__marker_icon___vDTMo")
ChatGPT advises me to extract the coordinates like this:
coordinates <- lapply(map_markers, function(marker) {
lat <- marker$getElementAttribute("data-lat")$value
lon <- marker$getElementAttribute("data-lon")$value
c(lat = as.numeric(lat), lon = as.numeric(lon))
})
Unfortunately this doesn't work: Error: attempt to apply non-function
. I assume there is no attribute within the extracted element called "data-lat"
or "data-lon"
. However, from inspecting the website's html I can't find anything which looks even vaguely like coordinates within the marker's code. Inspired by this post, I also checked the network tab and was able to find coordinates for the bounding box, but not for the two markers. Other posts talk about information hidden within script tags, etc. but this is beyond my abilities.
Any help on scraping the coordinates will be much appreciated!