从url中刮取lat lon的Python脚本

从url中刮取lat lon的Python脚本,python,Python,我有一个带有地址的数据库,我想运行一个脚本,从谷歌地图上抓取lat-lon信息并将其输入数据库 我想通过输入如下地址来调用站点: 其中,q=之前的所有内容都是静态的,之后的所有内容都由脚本生成 单击地址时,将返回以下信息: A)如何编程Python访问第一个网站,然后将返回的网站保存到一个变量,以及B)如何将@后面的两个数字拉入两个单独的变量?您也可以使用Google API v3。根据答案 导入urllib 导入simplejson 谷歌地理代码http://maps.googleapis

我有一个带有地址的数据库,我想运行一个脚本,从谷歌地图上抓取lat-lon信息并将其输入数据库

我想通过输入如下地址来调用站点:

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

单击地址时,将返回以下信息:


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

您也可以使用Google API v3。根据答案

导入urllib
导入simplejson
谷歌地理代码http://maps.googleapis.com/maps/api/geocode/json?'
def获取_坐标(查询,来自_传感器):
query=query.encode('utf-8')
参数={
“地址”:查询,
“传感器”:如果来自传感器,则为“真”,否则为“假”
}
url=googleGeocodeUrl+urllib.urlencode(参数)
json_response=urllib.urlopen(url)
response=simplejson.load(json_response.read())
如果响应['results']:
位置=响应['results'][0]['geometry']['location']
纬度,经度=位置['lat'],位置['lng']
打印查询、纬度、经度
其他:
纬度,经度=无,无
打印查询“”
打印
返回纬度、经度
u='http://maps.google.com/maps?q=1601+圆形剧场+公园道+山景+CA+94043'
获取_坐标(u[30:],'False')
1601+圆形剧场+公园道+山景+CA+94043 37.4240679-122.0859093
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')