gpt4 book ai didi

python - 查询字符串传递错误

转载 作者:行者123 更新时间:2023-11-28 04:08:02 24 4
gpt4 key购买 nike

我正在使用 url 获取给定城市的经度和纬度。当我经过“New York”作为字符串不会给出任何结果,但是当我通过“New%20York”时它会给出结果。如何将类似纽约的词传递给查询字符串?

current_location = 'New york' #not working
current_location2 = 'New%20York' # working
location_string = 'http://maps.googleapis.com/maps/api/geocode/json?address=' +current_location+ '&sensor=false'

最佳答案

使用urllib.quote()urllib.quote_plus()用它们的 %xx 等价物或 urllib.urlencode() 替换字符从您打算进入 URL 的变量字典构造查询字符串。示例:

>>> import urllib
>>> urllib.quote('New York')
'New%20York'
>>> urllib.quote_plus('New York')
'New+York'
>>> urllib.urlencode({'address': 'New York', 'sensor': 'false'})
'sensor=false&address=New+York'

关于python - 查询字符串传递错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9000399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com