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

发布于 2024-12-02 03:33:13 字数 895 浏览 0 评论 0原文

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

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

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

其中 USER_ID 是 Google 用于识别我身份的常数,MAP_ID 是由 链接生成的单独地图标识符右上角的 图标。

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

来自 Google Maps API HTTP 协议参考

地图源是用户创建的地图的源。

此 Feed 的完整 GET URI 为:

http://maps.google.com/maps/feeds/maps/默认/完整

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

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

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

感谢您的阅读

I want to create a script in Python which downloads the current KML files of all the Maps I created on Google Maps.

To do so manually, I can use this:

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

where USER_ID is a constant number Google uses to identify me, and MAP_ID is the individual map identifier generated by the link icon on top-right corner.

This is not very straightforward, because I have to manually browse "My Places" page on Google Maps, and get the links one by one.

From 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.

** The page says this service is no longer available, so I wonder if there is a way to do the same in the present.

So, the question is: Is there a way to get/download the list of MAP_IDs of all my maps, preferrably using Python?

Thanks for reading

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走干脆点 2024-12-09 03:33:13

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

The correct answer to this question involves using Google Maps Data API, HTML interface, which by the way is deprecated but still solves my need in a more official way, or at least more convincing than parsing a web page. Here it goes:

# 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

I arrived with this solution with a bunch of other questions in SO, and a lot of people helped me a lot, so I hope this code might help anyone else trying to do so in the future.

那支青花 2024-12-09 03:33:13

我发现的一种快速而肮脏的方法,完全跳过 Google Maps 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]+

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

)) 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

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

A quick and dirty way I have found, that skips Google Maps API completely and perhaps might brake in the near future, is this:

# 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]+

Of course it is only printing a numbered list, but from there to save a dictionary and/or automate KML download via &output=kml url trick it goes naturally.

)) 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

Of course it is only printing a numbered list, but from there to save a dictionary and/or automate KML download via &output=kml url trick it goes naturally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文