【问题标题】:Python Selenium - Webscrape Latitude and Longitude from the MapsPython Selenium - 从地图中抓取纬度和经度
【发布时间】:2017-10-28 12:06:48
【问题描述】:

我正在尝试从 URL 的地图中抓取经纬度

HTML 脚本如下所示,

<div class="map-container"><script>$(document).ready(function() {
  var $injector = angular.element(document.body).injector();
  var $compile = $injector.get('$compile');
  var $rootScope = $injector.get('$rootScope');
  var sfMap = $compile('<div data-sf-map data-center="center" data-zoom="15" data-markers="markers" data-scrollwheel="false" data-zoom-level-change="14"></div>');
  var scope = $rootScope.$new();

  scope.center = {
    latitude: 40.7515022729943,
    longitude: -74.0071970200125
  };

  scope.markers = [{
    id: 4673,
    latitude: 40.7515022729943,
    longitude: -74.0071970200125
  }];

  var $el = sfMap(scope);
  $('.listing-map .map-wrapper .map-container').append($el);
});

我尝试使用 Copy as XPath 选项,但它不适用于 这个案例。如何获取经纬度?

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    您可以尝试以下代码来获取纬度和经度值:

    import re
    
    script = driver.find_element_by_xpath('//div[@class="map-container"]/script').get_attribute('textContent')
    latitude =  re.search('(?<=latitude: )-*\d+.\d+', script).group(0)
    longitude = re.search('(?<=longitude: )-*\d+.\d+', script).group(0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 2017-10-19
      • 1970-01-01
      相关资源
      最近更新 更多