1

I'm following this post to calculate distance matrix so this is the code snippet

import urllib.request
from urllib.parse import quote  
import urllib.parse
import simplejson, urllib
import urllib.request
orig_coord = 19.12,19.45
dest_coord = 19.10,19.50
url = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + urllib.quote_plus(orig_coord)+ '&destinations=' + urllib.quote_plus(dest_coord) + '&mode=driving&language=en-EN&sensor=false'
#url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode=driving&language=en-EN&sensor=false".format(str(orig_coord),str(dest_coord))
#result= simplejson.load(urllib.urlopen(url))
result=urllib.request.urlopen(url)
driving_time = result['rows'][0]['elements'][0]['duration']['value']

I had tried these two method(other one with comments) but it never worked though, now with these code I get the error

runfile('C:/Users/admin/.spyder-py3/temp.py', wdir='C:/Users/admin/.spyder-py3')
Traceback (most recent call last):

  File "<ipython-input-22-30ed5be6f1c7>", line 1, in <module>
    runfile('C:/Users/admin/.spyder-py3/temp.py', wdir='C:/Users/admin/.spyder-py3')

  File "C:\Users\admin\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Users\admin\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/admin/.spyder-py3/temp.py", line 16, in <module>
    url = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + urllib.quote_plus(orig_coord)+ '&destinations=' + urllib.quote_plus(dest_coord) + '&mode=driving&language=en-EN&sensor=false'

AttributeError: module 'urllib' has no attribute 'quote_plus'
Domnick
  • 509
  • 8
  • 25
  • 1
    I think you have to replace the `urllib.quote_plus` calls with `urllib.parse.quote_plus` https://docs.python.org/3.1/library/urllib.parse.html – Jose A. García Nov 06 '17 at 13:05
  • @Jose used `from urllib.parse.quote_plus import quote_plus ` but it gave error `ModuleNotFoundError: No module named 'urllib.parse.quote_plus'; 'urllib.parse' is not a package` – Domnick Nov 07 '17 at 04:54
  • `from urllib.parse import quote_plus` works in python 3.1.6 – Jose A. García Nov 07 '17 at 08:40
  • Used the source code from [this](https://github.com/googlemaps/google-maps-services-python) and is working – Domnick Nov 07 '17 at 08:54

1 Answers1

1

The urlib doc states

Note The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.

So importing with urllib, urlib.parse, etc alltogether seems somewhat strange. quote_plus() should be in urlib.parse. Did you try with that one?

mikuszefski
  • 3,943
  • 1
  • 25
  • 38
  • I tried with `from urllib.parse import quote_plus ` but it gives error `AttributeError: module 'urllib' has no attribute 'quote_plus'` – Domnick Nov 07 '17 at 04:46
  • @Domnick Something in your comment is strange. If `quote_plus` would not exist in `urllib.parse` you should get an error message like `ImportError: cannot import name quote_plus`. Your error message implies that you still try to call `urllib.quote_plus()`! You should remove the `import urllib` completely from your code. – mikuszefski Nov 07 '17 at 10:09