如何使用urllib.parse.urlencode编码经纬度?
How to encode latitude and longitude using urllib.parse.urlencode?
我正在使用 Google API 获取附近咖啡店的 json 数据。为此,我需要将纬度和经度编码到 URL 中。
我正在使用 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
我正在使用 Google API 获取附近咖啡店的 json 数据。为此,我需要将纬度和经度编码到 URL 中。
我正在使用 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