Skip to content

Instantly share code, notes, and snippets.

@raykendo
Created February 19, 2020 05:54
Show Gist options
  • Save raykendo/036b8e870b24d061f286b99ade37bfe1 to your computer and use it in GitHub Desktop.
Save raykendo/036b8e870b24d061f286b99ade37bfe1 to your computer and use it in GitHub Desktop.
A Python Script for automating looking up addresses on Google Maps
#!/usr/bin/python
# viewGoogleMaps.py
# author: Ken Doman (github.com/raykendo)
# purpose: Looking up addresses through Google Maps automagically
# requirements: python 2 or 3, selenium for python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from os.path import join, isfile
import re, time
try:
from urllib.parse import quote, quote_plus
except:
from urllib import quote, quote_plus
# CONSTANTS
BASE_URL = "https://www.google.com/maps/place/{}"
#GECKODRIVER_PATH = "C:\\geckodriver\\geckodriver.exe"
CHROMEDRIVER_PATH = "C:\\chromedriver\\chromedriver.exe"
OUTPUT_IMAGE_FOLDER = "C:\\temp"
WAIT_FOR_ME = (By.CSS_SELECTOR, ".section-hero-header-image-hero > img")
SCREENSHOT_F_N_TEMPLATE = "{}_gmap{}.png"
LAT_LONG_REGEX = re.compile(r"\/@(?P<latitude>-?\d+\.\d+),(?P<longitude>-?\d+\.\d+),\d+\w\/")
# get screenshot filename based on address string
def get_screenshot_filename(addressLine):
clean_addr_line = re.sub(r"[\s\.',\\\/]+", "_", addressLine)
file_name = SCREENSHOT_F_N_TEMPLATE.format(clean_addr_line, "")
index = 0
# cycle through file names if they already exist
while isfile(join(OUTPUT_IMAGE_FOLDER, file_name)):
index += 1
file_ending = "_({})".format(index)
file_name = SCREENSHOT_F_N_TEMPLATE.format(clean_addr_line, file_ending)
return join(OUTPUT_IMAGE_FOLDER, file_name)
def lookupAddress(addressLine, browser=None):
# if browser not preloaded, load chrome browser
if browser == None:
with webdriver.Chrome(executable_path=CHROMEDRIVER_PATH) as thisbrowser:
results = lookupAddress(addressLine, browser=thisbrowser)
return results
# load the url into the browser
url = BASE_URL.format(quote_plus(addressLine))
browser.get(url)
# wait for part of Google Maps results are loaded.
wait = WebDriverWait(browser, 60)
wait.until(EC.element_to_be_clickable(WAIT_FOR_ME))
# check if Google Maps actually loaded
assert "Google Maps" in browser.title
# add an extra bit of time
time.sleep(3)
# take a picture
file_name = get_screenshot_filename(addressLine)
browser.save_screenshot(file_name)
# pull lat/long from url
url_search = LAT_LONG_REGEX.search(browser.current_url)
results = {}
if url_search and url_search.group("latitude") and url_search.group("longitude"):
results["latitude"] = float(url_search.group("latitude"))
results["longitude"] = float(url_search.group("longitude"))
return results
if __name__ == "__main__":
# as a test, look up this address
test_address = "11222 Dilling St, North Hollywood, CA 91602"
results = lookupAddress(test_address)
print ("The location of the address {0} is Latitude: {1[latitude]}, Longitude: {1[longitude]}".format(test_address, results))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment