【问题标题】:(ValueError: unknown url type) while opening json (python3)(ValueError:未知的url类型)同时打开json(python3)
【发布时间】:2016-02-06 05:30:50
【问题描述】:
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。 “uh=urllib.request.urlopen(url)”出现错误,无法打开url。我的代码有什么问题?

【问题讨论】:

    标签: json python-3.x url


    【解决方案1】:

    您在https 之后缺少:

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

    【讨论】:

    • 天哪,我犯了这个愚蠢的错误。我想我可能是个盲人。顺便说一句,非常感谢。
    猜你喜欢
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2021-03-11
    • 1970-01-01
    相关资源
    最近更新 更多