【问题标题】:Python script to scrape lat lon from url用于从 url 中抓取 lat lon 的 Python 脚本
【发布时间】:2015-03-11 03:47:58
【问题描述】:

我有一个包含地址的数据库,我想运行一个脚本来从 Google 地图中抓取纬度信息并将其输入到数据库中。

我想通过输入这样的地址来呼叫一个站点: http://maps.google.com/maps?q=1600+Amphitheatre+Parkway+Mountain+View+CA+94043

q= 之前的所有内容都是静态的,之后的所有内容都是由脚本生成的。

当您单击地址时,将返回: https://www.google.com/maps/place/1600+Amphitheatre+Pkwy,+Mountain+View,+CA+94043/@37.4224879,-122.08422,17z/data=!3m1!4b1!4m2!3m1!1s0x808fba027820e5d9:0x60a90600ff6e7e6e

我如何 A) 编写 Python 以访问第一个网站,然后将返回的网站保存到一个变量中,然后 B) 将 @ 后面的两个数字拉成 2 个单独的变量?

【问题讨论】:

    标签: python


    【解决方案1】:

    您也可以使用 Google API v3。基于答案GoogleMaps API -address to coordinates (latitude,longitude)

    import urllib
    import simplejson
    googleGeocodeUrl = 'http://maps.googleapis.com/maps/api/geocode/json?'
    def get_coordinates(query, from_sensor):
        query = query.encode('utf-8')
        params = {
            'address': query,
            'sensor': "true" if from_sensor else "false"
        }
        url = googleGeocodeUrl + urllib.urlencode(params)
        json_response = urllib.urlopen(url)
        response = simplejson.loads(json_response.read())
        if response['results']:
            location = response['results'][0]['geometry']['location']
            latitude, longitude = location['lat'], location['lng']
            print query, latitude, longitude
        else:
            latitude, longitude = None, None
            print query, "<no results>"
            print
        return latitude, longitude
    
    
    u  = 'http://maps.google.com/maps?q=1601+Amphitheatre+Parkway+Mountain+View+CA+94043'
    get_coordinates(u[30:],'False')
    

    1601+圆形剧场+百汇+山景+景观+CA+94043 37.4240679 -122.0859093

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多