Search code examples
rweb-scrapinggeolocation

Get latitude and longitude from map object using selector gadget in r


I'm doing data scraping(for the first time) in R by using selectorGadget extension for chrome ,which uses the package "rvest" this is the reference from which I'm doing

and from this website I'm trying to fetch data

this is my code

#Specifying the url for desired website to be scrapped
url <- 'http://www.magicbricks.com/property-for-sale/Multistorey-Apartment-real-estate-Mumbai'

#Reading the HTML code from the website
webpage <- read_html(url)

map_data_html <- html_nodes(webpage,'.iconMap .stop-propagation')
map <- html_text(map_data_html)
head(map)

but this gives me only the text as "map" , I want to access the lat and long attribute inside this map. Any suggestion?


Solution

  • Probably not optimal but this is one way to get the lat/lon values:

    map_data_html <- html_nodes(webpage,'.iconMap .stop-propagation')
    map = html_attr(map_data_html,"data-link") # get the data-link part
    
    lat = as.numeric(str_match(map, "lat=(.*?)&longt")[,2]) # find the lat
    lon = as.numeric(str_match(map, "longt=(.*?)&projectOr")[,2]) # find the lon