Pessoal estou utilizando o código abaixo para consultar no google latitude e longitude baseado no endereço, gostaria de saber como faço para passar para a URL o endereço em forma parâmetro? O código abaixo funciona perfeitamente, pois estou passando os parâmetros fixos:
import urllib.request
import json
>
>
> with urllib.request.urlopen("https://maps.googleapis.com/maps/api/geocode/json?address=140+Rua+Cica,Curitiba%C3%AD,+PR&key=AIzaSyBKP7Lndi1G9-O1NpCwV7y_QV5i7tMBx9o") as url:
> with
>
> s = url.read()
> jsonResponse = json.loads(s)
> test = json.dumps([s['geometry']['location'] for s in jsonResponse['results']], indent=3)
> print(test)
Mas quando tento passar os parâmetros para URL da erro:
a= 140 b='Rua Cica' c = 'Curitiba' d = 'PR'
with urllib.request.urlopen("https://maps.googleapis.com/maps/api/geocode/%/%/%/%/json?address= % a+% b,% c%C3%AD,+d&key=AIzaSyBKP7Lndi1G9-O1NpCwV7y_QV5i7tMBx9o") as url:
O erro:
C:\Users\Usuário\PycharmProjects\validacep\venv\Scripts\python.exe C:/Users/Usuário/PycharmProjects/validacep/XXX.py Traceback (most recent call last): File "C:/Users/Usuário/PycharmProjects/validacep/XXX.py", line 11, in with urllib.request.urlopen("https://maps.googleapis.com/maps/api/geocode/%/%/%/%/json?address= % a+% b,% c%C3%AD,+d&key=AIzaSyBKP7Lndi1G9-O1NpCwV7y_QV5i7tMBx9o") as url: File "C:\Users\Usuário\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\Users\Usuário\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open response = meth(req, response) File "C:\Users\Usuário\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "C:\Users\Usuário\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 569, in error return self._call_chain(*args) File "C:\Users\Usuário\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain result = func(*args) File "C:\Users\Usuário\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 400: Bad Request