我正在使用Google API获取附近咖啡店的json数据。为此,我需要将纬度和经度编码为URL。
所需的网址:https : //maps.googleapis.com/maps/api/place/textsearch/json?query=coffee&location=22.303940,114.170372&radius=1000&maxprice=3&key=myAPIKey
我使用urlencode获取的URL:https ://maps.googleapis.com/maps/api/place/textsearch/json ? query = coffee & location = 22.303940%2C114.170372 & radius = 1000 & maxprice =3& key = myAPIKEY
如何删除URL中的“%2C”?(我在下面显示了我的代码)
serviceurl_placesearch = 'https://maps.googleapis.com/maps/api/place/textsearch/json?'
parameters = dict()
query = input('What are you searching for?')
parameters['query'] = query
parameters['location'] = "22.303940,114.170372"
while True:
radius = input('Enter radius of search in meters: ')
try:
radius = int(radius)
parameters['radius'] = radius
break
except:
print('Please enter number for radius')
while True:
maxprice = input('Enter the maximum price level you are looking for(0 to 4): ')
try:
maxprice = int(maxprice)
parameters['maxprice'] = maxprice
break
except:
print('Valid inputs are 0,1,2,3,4')
parameters['key'] = API_key
url = serviceurl_placesearch + urllib.parse.urlencode(parameters)
我添加了这段代码以使URL正常工作,但是我认为这不是一个长期解决方案。我正在寻找更长期的解决方案。
urlparts = url.split('%2C')
url = ','.join(urlparts)
你可以加 safe=","
import urllib.parse
parameters = {'location': "22.303940,114.170372"}
urllib.parse.urlencode(parameters, safe=',')
结果
location=22.303940,114.170372
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句