计算距离矩阵时出错

时间:2017-11-06 12:54:37

标签: python urllib3 google-distancematrix-api

我正在关注this帖子来计算距离矩阵 所以这是代码片段

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']

我曾尝试过这两种方法(另一种方法有评论) 但它从未奏效过, 现在使用这些代码我得到了错误

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'

1 个答案:

答案 0 :(得分:0)

urlib doc个州

  

注意urllib模块已拆分为多个部分,并在Python 3中重命名为urllib.request,urllib.parse和urllib.error。

因此,使用urlliburlib.parse等进行导入似乎有些奇怪。 quote_plus()应该在urlib.parse。你试过那个吗?