Search code examples
pythonseleniumselenium-webdriverweb-scrapinglatitude-longitude

How to get the latitude and longitude (map icon) from a website using selenium?


I am trying to scrape a website to find the latitude and longitude of that place. The map icon is available on the website. The map icon when clicked redirects to the justdial map. The icon in the website is as

enter image description here

The inspect element for the map icon is

<a title="Map for JICE Academy For Excellence Pvt Ltd, Vijayanagar" href="javascript:;" onclick="view_map('080PXX80.XX80.121102122047.B3M5', 'map', 'Bangalore');
                                                        _ct('map', 'dtpg');">
                                        <span class="ico-mapicn"></span>
                                        <span class="wrtrvtxt">Map</span>
</a>

It seems to me like view_map is a function and its first argument is the coordinates. I scraped this using

url = "https://www.justdial.com/Bangalore/Race-Coaching-Institute-Pvt-Ltd-Hosur-Road/080PXX80-XX80-170420162628-Z6C1_BZDET?xid=QmFuZ2Fsb3JlIEJhbmsgRXhhbSBUdXRvcmlhbHM="
wd = webdriver.Chrome(chrome_path)
wd.get(url)
ele = wd.find_element_by_xpath('//a[@class="mapicn"]')
print(ele.get_attribute('onclick'))

But I am unable to figure out what format it is. Is there any other way I can get the exact lat and log of that place?


Solution

  • The latitude and longitude is not available in the page, to get it you can emulate the post request to https://..../functions/maps.php or intercept the XMLHttpRequest

    url = "https://......."
    wd = webdriver.Chrome()
    wd.get(url)
    wd.execute_script('''
    (function(open) {
        window.XMLHttpRequest.prototype.open = function() {
            this.addEventListener("readystatechange", function() {
                if(this.readyState == 4 && this.responseURL.indexOf('maps.php') > -1){
                    window.latlong = this.responseText
                }
            }, false);
            open.apply(this, arguments);
        };
    })(window.XMLHttpRequest.prototype.open);
    ''')
    
    wd.find_element_by_xpath('//a[@class="mapicn"]').click()
    
    latlong = wd.execute_async_script('var theData = arguments[0]; theData(latlong)')
    print(latlong)
    # {"lil":"12.889135400000","lon":"77.639586800000"}