如何使用Google Map Feed API获取使用Python的Google地图列表?

时间:2011-08-29 20:33:47

标签: python google-maps kml

我想在Python中创建一个脚本,下载我在Google地图上创建的所有地图的当前KML文件。

要手动完成,我可以使用:

http://maps.google.com.br/maps/ms?msid=USER_ID.MAP_ID&msa=0&output=kml

其中USER_ID是Google用于识别我的常数,而MAP_ID是由右上角的link图标生成的单个地图标识符。

这不是很简单,因为我必须手动浏览Google地图上的“我的地点”页面,然后逐个获取链接。

来自Google Maps API HTTP Protocol Reference

  

地图Feed是用户创建的地图的供稿。

     

此Feed的完整GET URI为:

     

http://maps.google.com/maps/feeds/maps/default/full

     

此Feed返回经过身份验证的用户的所有地图列表。

**页面上说这项服务已不再可用,所以我想知道目前是否有办法做同样的事情。

所以,问题是:有没有办法获取/下载我所有地图的MAP_ID列表,最好使用Python?

感谢您阅读

2 个答案:

答案 0 :(得分:1)

这个问题的正确答案涉及使用谷歌地图数据API,HTML界面,这种方式已被弃用,但仍以更正式的方式解决了我的需求,或者至少比解析网页更有说服力。在这里:

# coding: utf-8

import urllib2, urllib, re, getpass

username = 'heltonbiker'
senha = getpass.getpass('Senha do usuário ' + username + ':')

dic = {
        'accountType':      'GOOGLE',
        'Email':            (username + '@gmail.com'),
        'Passwd':           senha,
        'service':          'local',
        'source':           'helton-mapper-1'
        }
url = 'https://www.google.com/accounts/ClientLogin?' + urllib.urlencode(dic)
output = urllib2.urlopen(url).read()
authid = output.strip().split('\n')[-1].split('=')[-1]

request = urllib2.Request('http://maps.google.com/maps/feeds/maps/default/full')
request.add_header('Authorization', 'GoogleLogin auth=%s' % authid)
source = urllib2.urlopen(request).read()

for link in re.findall('<link rel=.alternate. type=.text/html. href=((.)[^\1]*?)>', source):
    s = link[0]
    if 'msa=0' in s:
        print s

我带着这个解决方案带来了很多其他问题,很多人帮助了我很多,所以我希望这些代码可以帮助其他人在将来尝试这样做。

答案 1 :(得分:0)

我发现的一种快速而肮脏的方式,完全跳过谷歌地图API,也许可能在不久的将来制动,是这样的:

# coding: utf-8

import urllib, re
from BeautifulSoup import BeautifulSoup as bs

uid = '200931058040775970557'
start = 0
shown = 1

while True:
    url = 'http://maps.google.com/maps/user?uid='+uid+'&ptab=2&start='+str(start)
    source = urllib.urlopen(url).read()
    soup = bs(source)
    maptables = soup.findAll(id=re.compile('^map[0-9]+$'))
    for table in maptables:
        for line in table.findAll('a', 'maptitle'):
            mapid = re.search(uid+'\.([^"]*)', str(line)).group(1)
            mapname = re.search('>(.*)</a>', str(line)).group(1).strip()[:-2]
            print shown, mapid, mapname
            shown += 1

            # uncomment if you want to download the KML files:
            # urllib.urlretrieve('http://maps.google.com.br/maps/ms?msid=' + uid + '.' + str(mapid) +
                               '&msa=0&output=kml', mapname + '.kml')                

    if '<span>Next</span>' in str(source):
        start += 5
    else:
        break

当然它只打印一个编号列表,但是从那里保存字典和/或通过&output=kml url技巧自动化KML下载它自然而然。