Skip to main content
 首页 » 编程设计

python之如何使用 Google Map Feed API 通过 Python 获取我的 Google map 列表

2025年05月04日23bhlsheji

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

要手动执行此操作,我可以使用:

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

其中 USER_ID 是 Google 用来识别我的常数,MAP_ID 是由顶部的 link 图标生成的个人 map 标识符-右角。

这不是很简单,因为我必须手动浏览 Google map 上的“我的位置”页面,并逐一获取链接。

来自 Google Maps API HTTP Protocol Reference :

The Map Feed is a feed of user-created maps.

This feed's full GET URI is:

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

This feed returns a list of all maps for the authenticated user.

** 网页上说这个服务不再可用,所以我想知道目前是否有办法做到这一点。

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

感谢阅读

请您参考如下方法:

这个问题的正确答案涉及使用 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 中提出了这个解决方案,还有很多其他问题,很多人帮助了我很多,所以我希望这段代码可以帮助将来尝试这样做的其他人。