0

I have the url: https://maps.google.com/maps?ll=44.864505,-93.44873&z=18&t=m&hl=en&gl=US&mapclient=apiv3

I wanted to extract the lat/long from the url, so that I have 44.864505,-93.44873.

So far I have (^[maps?ll=]*$|(?<=\?).*)* which gives me ll=44.864505,-93.44873&z=18&t=m&hl=en&gl=US&mapclient=apiv3 but this needs impovement. I have been trying to use pythex to work this out, but I am stuck.

Any suggestions? Thanks

a1234
  • 761
  • 3
  • 12
  • 23
  • See https://stackoverflow.com/questions/5074803/retrieving-parameters-from-a-url – The fourth bird Jul 17 '19 at 19:15
  • Why not split the string by the 'll=' and '&' if that is common nomenclature url = 'https://maps.google.com/maps?ll=44.864505,-93.44873&z=18&t=m&hl=en&gl=US&mapclient=apiv3' lat,long = url.split("ll=")[1].split("&")[0].split(",") – Cody Glickman Jul 17 '19 at 19:41

1 Answers1

7

I wouldn't use regex, I'd use urlparse

For Python2:

import urlparse

url = 'https://maps.google.com/maps?ll=44.864505,-93.44873&z=18&t=m&hl=en&gl=US&mapclient=apiv3'
parsed = urlparse.urlparse(url)
params = urlparse.parse_qs(parsed.query)

print(params['ll'])

prints:

['44.864505,-93.44873']

For Python3 (urllib.parse) :

import urllib.parse as urlparse

url = 'https://maps.google.com/maps?ll=44.864505,-93.44873&z=18&t=m&hl=en&gl=US&mapclient=apiv3'
parsed = urlparse.urlparse(url)
params = urlparse.parse_qs(parsed.query)

print(params['ll'])

prints:

['44.864505,-93.44873']
That1Guy
  • 7,075
  • 4
  • 47
  • 59