Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script to query google maps and get the resulting URL [closed]

Say I wanted to search "The White House" in google maps. The resulting URL would be "https://www.google.com/maps/place/The+White+House/@38.8976805,-77.0387185,17z/data=!3m1!4b1!4m5!3m4!1s0x89b7b7bcdecbb1df:0x715969d86d0b76bf!8m2!3d38.8976763!4d-77.0365298". I want to simulate this in python. How would I go about doing this? So ideally, I'd pass in "The White House" and print out the resulting URL.

My end goal is to find the coordinates from a place name.

like image 824
lena Avatar asked Jan 20 '26 01:01

lena


1 Answers

I want to find the coordinates

you can scrape it like this:

import requests
import ast

def google_maps(req): 

    req.replace(' ','+')
    uri = 'https://www.google.com/maps/place/'
    url = uri+req
    ret = requests.get(url).text
    scrape = '['+ret.split('cacheResponse([[')[1].split(',[')[0].split(',',1)[1]
    location = ast.literal_eval(scrape)
    print location

google_maps('the white house')


>>>
[-77.0365298, 38.8976763]

to get a better sense of the data returned by the request you can do:

print ret[:10000]

to get the first 10000 characters where you'll find the prefix that begins the first split() operation:

cacheResponse([[

likewise:

google_maps('the pentagon')
>>>
[-77.0562669, 38.8718568]

google_maps('the golden gate bridge')
>>>
[-122.4782551, 37.8199286]
etc.
like image 169
litepresence Avatar answered Jan 22 '26 16:01

litepresence



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!