繁体   English   中英

如何使用硒从网站获取经纬度(地图图标)?

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

我正在尝试抓取一个网站以查找该地点的纬度和经度。 地图图标可在网站上找到。 单击时地图图标重定向到 justdial 地图。 网站中的图标如下

在此处输入图像描述

地图图标的检查元素是

<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>

在我看来 view_map 是一个函数,它的第一个参数是坐标。 我用这个刮了这个

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'))

但我无法弄清楚它是什么格式。 有没有其他方法可以获得那个地方的确切纬度和日志?

纬度和经度在页面中不可用,要获取它,您可以模拟 post 请求到https://..../functions/maps.php或拦截 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"}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM