Pass parameter in URL

0

Personal I am using the code below to query on google latitude and longitude based address, I would like to know how do I pass to the URL address in parameter form? The code below works perfectly because I'm passing the fixed parameters:

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)

But when I try to pass the parameters to error URL:

a = 140 b = 'Cica Street' 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:

The error:

C: \ Users \ User \ PycharmProjects \ validacep \ venv \ Scripts \ python.exe C: /Users/User/PycharmProjects/validacep/XXX.py Traceback (most recent call last):   File "C: /Users/User/PycharmProjects/validacep/XXX.py", line 11, in     with urllib.request.urlopen (" link % a + % b,% c% AD, + d & key = AIzaSyBKP7Lndi1G9-O1NpCwV7y_QV5i7tMBx9o ") as url:   File "C: \ Users \ User \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ urllib \ request.py", line 222, in urlopen     return opener.open (url, date, timeout)   File "C: \ Users \ User \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ urllib \ request.py", line 531, in open     response = meth (req, response)   File "C: \ Users \ User \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ urllib \ request.py", line 641, in http_response     'http', request, response, code, msg, hdrs)   File "C: \ Users \ User \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ urllib \ request.py", line 569, in error     return self._call_chain (* args)   File "C: \ Users \ User \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ urllib \ request.py", line 503, in _call_chain     result = func (* args)   File "C: \ Users \ User \ 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

    
asked by anonymous 22.05.2018 / 14:40

0 answers