查询字符串传递错误

Evl*_*ark 1 html python

我正在使用网址获取特定城市的经度和纬度.当我通过"纽约"作为字符串时,它不会给出任何结果,但是当我通过"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'
Run Code Online (Sandbox Code Playgroud)

Ign*_*ams 5

urllib.urlencode() 将为您处理查询字符串的生成.


Ada*_*man 5

使用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'
Run Code Online (Sandbox Code Playgroud)