Python 程序调用 Google Maps api,在 urllib.urlencode 上给出错误



我正在尝试从Python 3.8调用Google Map API。

import urllib
import json
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = input('Enter location: ')
if len(address) < 1:
break
url = serviceurl + urllib.urlencode({'sensor': 'false',
'address': address})
print('Retrieving', url)
uh = urllib.urlopen(url)
data = uh.read()
print('Retrieved', len(data), 'characters')
try:
js = json.loads(str(data))
except:
js = None
if 'status' not in js or js['status'] != 'OK':
print('==== Failure To Retrieve ====')
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)
location = js['results'][0]['formatted_address']
print(location)

编译错误在urllib.urlencodeurllib.urlopen.。

它是

在"init_.pyi"中找不到引用"urlencode">

代码中存在几个问题。首先,urllib在Python 3中被拆分了,请查看"模块"没有属性"urlencode"。

其次,您需要在地理编码请求中使用 HTTPS 协议,并确保使用有效的 API 密钥:

serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?key=AIza****sYjs&'

第三,将json.loads(str(data))替换为仅json.loads(data)

完整修订的代码如下:

import urllib.parse
import urllib.request
import json
serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?key=AIza****sYjs&'
while True:
address = input('Enter location: ')
if len(address) < 1:
break
url = serviceurl + urllib.parse.urlencode({'sensor': 'false',
'address': address})
print('Retrieving', url)
uh = urllib.request.urlopen(url)
data = uh.read()
print('Retrieved', len(data), 'characters')
try:
js = json.loads(data)
except:
js = None
if 'status' not in js or js['status'] != 'OK':
print('==== Failure To Retrieve ====')
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)
location = js['results'][0]['formatted_address']
print(location)

例如"法国巴黎"的输出:

Enter location: paris,france
Retrieving https://maps.googleapis.com/maps/api/geocode/json?key=AIza****sYjs&sensor=false&address=paris%2Cfrance
Retrieved 1690 characters
{
"results": [
{
"address_components": [
{
"long_name": "Paris",
"short_name": "Paris",
"types": [
"locality",
"political"
]
},
{
"long_name": "Paris",
"short_name": "Paris",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "u00cele-de-France",
"short_name": "IDF",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "France",
"short_name": "FR",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Paris, France",
"geometry": {
"bounds": {
"northeast": {
"lat": 48.9021449,
"lng": 2.4699208
},
"southwest": {
"lat": 48.815573,
"lng": 2.224199
}
},
"location": {
"lat": 48.856614,
"lng": 2.3522219
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 48.9021449,
"lng": 2.4699208
},
"southwest": {
"lat": 48.815573,
"lng": 2.224199
}
}
},
"place_id": "ChIJD7fiBh9u5kcRYJSMaMOCCwQ",
"types": [
"locality",
"political"
]
}
],
"status": "OK"
}
lat 48.856614 lng 2.3522219
Paris, France

希望这有帮助!

相关内容

  • 没有找到相关文章

最新更新