Skip to content

Instantly share code, notes, and snippets.

@endolith
Created October 16, 2012 02:29
Show Gist options
  • Save endolith/3896948 to your computer and use it in GitHub Desktop.
Save endolith/3896948 to your computer and use it in GitHub Desktop.
Export Google Maps starred locations
# -*- coding: utf-8 -*-
"""
Go to Google Bookmarks: https://www.google.com/bookmarks/
On the bottom left, click "Export bookmarks": https://www.google.com/bookmarks/bookmarks.html?hl=en
After downloading the html file, run this script on it to generate a KML.
"""
from lxml.html import document_fromstring
import simplekml
from urllib2 import urlopen
import re
import time
filename = r'GoogleBookmarks.html'
with open(filename) as bookmarks_file:
data = bookmarks_file.read()
kml = simplekml.Kml()
# Hacky and doesn't work for all of the stars:
lat_re = re.compile('markers:[^\]]*latlng[^}]*lat:([^,]*)')
lon_re = re.compile('markers:[^\]]*latlng[^}]*lng:([^}]*)')
coords_in_url = re.compile('\?q=(-?\d{,3}\.\d*),\s*(-?\d{,3}\.\d*)')
doc = document_fromstring(data)
for element, attribute, url, pos in doc.body.iterlinks():
if 'maps.google' in url:
description = element.text or ''
print description.encode('UTF8')
print u"URL: {0}".format(url)
if coords_in_url.search(url):
# Coordinates are in URL itself
latitude = coords_in_url.search(url).groups()[0]
longitude = coords_in_url.search(url).groups()[1]
else:
# Load map and find coordinates in source of page
try:
sock = urlopen(url.replace(' ','+').encode('UTF8'))
except Exception, e:
print 'Connection problem:'
print repr(e)
print 'Waiting 2 minutes and trying again'
time.sleep(120)
sock = urlopen(url.replace(' ','+').encode('UTF8'))
content = sock.read()
sock.close()
time.sleep(3) # Don't annoy server
try:
latitude = lat_re.findall(content)[0]
longitude = lon_re.findall(content)[0]
except IndexError:
print '[Coordinates not found]'
print
continue
print latitude, longitude
try:
kml.newpoint(name=description,
coords=[(float(longitude), float(latitude))])
except ValueError:
print '[Invalid coordinates]'
print
kml.save("GoogleBookmarks.kml")
@kate1212klim
Copy link

fantastic! i'm learning python and it's really fascinating how it can help solve problems) Hope I can write such helpful codes soon :) Good luck!

@thany
Copy link

thany commented Mar 24, 2022

The idea here is great, but unfortunately Google have been stirring around in their products, and the only way since a little while to export saved places, is via Takeout - select "Saved".

This will produce a file containing all saved places, sure enough, but:

  1. They are in CSV format, not JSON or HTML.
  2. They do not contain lat/lon, but instead a link that presumably be used against the Places API (example)

So this script will have probably lost its usefulness some years ago 😞

@endolith
Copy link
Author

endolith commented Mar 24, 2022

@thany From Google Takeout, Saved Places.json and Labeled places.json both contain lat/lon and can be converted using https://mygeodata.cloud/converter/json-to-gpx

I've been using that technique lately, so yeah this script is probably obsolete, it's from 2012.

@jordymeow
Copy link

jordymeow commented Dec 8, 2022

Unfortunately, impossible to get the GPS coordinates for anything else than the locations with stars. Personally, I used exportgooglemaps.com; this tool takes the CSV and JSON from Google Takeout and create new files. You get the Place ID, GPS coordinates, and a few more columns, but that's what I needed the most. Not sure why Google doesn't give better files...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment