Home > Mobile >  Scrape locations (coordinates) of clickable map on website posting using R
Scrape locations (coordinates) of clickable map on website posting using R

Time:09-10

I would like to get the location (coordinates) in latitude and longitude from the clickable map on this web page and other similar pages. I am not sure how to go about this using rvest or a scraping package since the Google Maps link with coordinates as shown below only appears if I click on the Ver Mapa button in blue.

https://www.google.com/maps/embed/v1/search?q=-26.106150272877,-56.89951582606415&key=AIzaSyBULWdjzDkVfa-hG9q-_TIv3MuhDdAA1Zc&zoom=14

under <div id=map-canvas">

CodePudding user response:

You are right, that the link is executed only when clicking the button. You also cannot access the link through rvest, because it is stored inside of a script. However, the script is loaded together with the HTML. Which means, it is accessible through the source-text of the website.

The website somehow had to know, where the coordinates are, to succesfully query google maps. Since you are only interested in the coordinates (I suppose), here is a code snippet that should get you started. It downloads the html as text and through regex filters the relevant info from the stored google request.

You can play around with the regex here: https://regex101.com/r/XIagYl/1

What I did was just navigate to the source text of the website and ctrl-f "google.com/maps" and there the link was sitting in the script. :)

text <- httr::GET("https://clasipar.paraguay.com/inmuebles/propiedades-rurales/granja-sobre-asfalto-con-infraestructura-completa-2093966") %>% httr::content(as ="text")

stringr::str_extract_all(text,"(?<=q=).*(?=\\&key)")
  • Related