(ValueError:未知的url类型)打开json(python3)

时间:2016-02-06 05:30:51

标签: json python-3.x url

import urllib
import json

serviceurl='https//maps.googleapis.com/maps/api/geocode/json?'

while True:
    address=input('Enter location: ')
    if len(address)<1 :
        break
    url =serviceurl+urllib.parse.urlencode({'sensor':'false','address':address})
    print('retriving',url)
    uh=urllib.request.urlopen(url)
    data=uh.read().decode('utf8')
    print ('Retrieved',len(data),'characters')
    try:
        js=json.loads(str(data))


    except:
        json=None
    if 'status' not in js or js['status'] !='OK' :
        print('fail~~')
        print(data)
        continue
    print(json.dumps(js,indent=4))
    lat=js["results"][0]["geometry"]["location"]['lat']
    lng=js["results"][0]["geometry"]["location"]['lng']
    print(lat,lat,lng,lng)
    print(location)

我正在访问Google的GeoJSON API。 &#34; uh = urllib.request.urlopen(url)&#34;发生错误,无法打开网址。我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

:之后您错过了https

https://maps.googleapis.com/maps/api/geocode/json?
 HERE^