Search code examples
pythonextracttext-extraction

How to extract longitude and latitude from a link


From the following link, I am trying to extract the longitude and latitude. I found similar posts but not one with the same format. I'm new to regex/text manipulation and would appreciate any guidance on how I might do this using Python. The output I'd like to have from this example is

latitude = 40.744221 
longitude = -73.982854

Many thanks in advance.

https://maps.googleapis.com/maps/api/staticmap?scale=1&center=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw


Solution

  • Python has a module for parsing URLs in the standard library

    from urllib import parse
    
    # Split off the query
    _, query_string = parse.splitquery("https://maps.googleapis.com/maps/api/staticmap?scale=1&center=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw")
    
    # Parse the query into a dict
    query = parse.parse_qs(query_string)
    
    # You can now access the query using a dict lookup
    latlng = query["center"]
    
    # And to get the values (selecting 0 as it is valid for a query string to contain the same key multiple times).
    latitude, longitude = latlng[0].split(",")
    

    For this usecase I would avoid regular expressions. The urllib module is more explicit, will handle all aspects of URL encoding and is well tested.

    Another third party module for handling URLs is the excellent YARL.