2011-08-29 24 views
2

我想在Python中创建一个脚本,该脚本下载我在Google地图上创建的所有地图的当前KML文件。如何使用Google Map Feed API来获取我的Google Maps使用Python的列表?

要那么做手工,我可以用这个:

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

其中USER_ID是常数谷歌使用,以确定我,MAP_ID是在右上角的link图标生成单独的地图标识。

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

Google Maps API HTTP Protocol Reference

地图供稿是用户创建的地图的进料。

这种饲料的完整GET URI是:

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

这种饲料返回经过验证的用户的所有地图的清单。

**该页面表示此服务不再可用,所以我想知道现在是否有办法做同样的事情。

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

感谢您阅读

回答

1

这个问题的正确答案涉及到使用Google Maps Data 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 

我带着这个解决方案有一堆的SO其他问题,有很多人帮助了我很多,所以我希望这个代码可以帮助其他人试图在这样做未来。

+0

我也一直在考虑这个...此端点是否仍在工作? – docchang

+0

@docchang好吧,你可以测试它,但现在已经有一段时间了......自那时起,Google中很多事情都发生了变化。我的回答是“好吧,我真的不知道”... – heltonbiker

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保存字典和/或自动化KML下载网址欺骗自然而然。

+0

我现在会接受我自己的答案,但我真的打算将其替换为另一个更强大(减少对Google页面布局的依赖) – heltonbiker

相关问题