Python 使用Google'时出错;s findplacefromtext-无效URL

Python 使用Google'时出错;s findplacefromtext-无效URL,python,google-maps,google-api,urllib,street-address,Python,Google Maps,Google Api,Urllib,Street Address,我正在尝试使用谷歌API的findplacefromtext获取地址的详细信息。我的代码是这样的: def get_google_address(address): API_KEY = 'MY_API_KEY' URL = ('https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input={add}&inputtype=textquery&fields=formatted_addre

我正在尝试使用谷歌API的
findplacefromtext
获取地址的详细信息。我的代码是这样的:

def get_google_address(address):
    API_KEY = 'MY_API_KEY'
    URL = ('https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input={add}&inputtype=textquery&fields=formatted_address,name,geometry&key={API_KEY}').format(add=address,API_KEY=API_KEY)
    print(URL)
    response = urllib.request.urlopen(URL)
    data = json.load(response)
    return (data)
如果我按如下方式调用函数:
get_google_address('1600圆形剧场pkwy mountain view ca')
,我会得到以下错误:

InvalidURL: URL can't contain control characters. '/maps/api/place/findplacefromtext/json?input=1600 amphitheatre pkwy mountain view ca&inputtype=textquery&fields=formatted_address,name,geometry&key=API_KEY' (found at least ' ')

但是,如果我将URL粘贴到浏览器中,它就会工作。请让我知道我错过了什么。URL如下所示:
https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=1600 圆形剧场pkwy mountain view ca&inputtype=textquery&fields=formatted_address、name、geometry&key=API_key
根据@geocodezip的评论,需要编码的URL:

dict1 = {'input':address, 'key':API_KEY}
qstr = urllib.parse.urlencode(dict1) 
URL = 'https://maps.googleapis.com/maps/api/place/findplacefromtext/json?inputtype=textquery&fields=formatted_address,name,geometry&'
URL = URL + qstr
response = urllib.request.urlopen(URL)
data = json.load(response)
return (data)

听起来你需要对输入字符串进行URL编码。@geocodezip-你能把你的评论作为答案发表吗?我会接受这个正确答案。我可以通过对查询字符串进行编码来解决这个问题。只需接受您自己的答案,这比我的评论要好