Pass parameter in URL

Asked

Viewed 304 times

1

I am using the code below to search in google latitude and longitude based on the address, I would like to know how to pass to the URL the address in parameter form? The code below works perfectly, because I am 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='Street 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:

The mistake:

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("https://maps.googleapis.com/maps/api/geocode/%/%/%/%/json? address= % a+% b,% c%C3%AD,+d&key=Aizasybkp7lndi1g9-O1npcwv7y_qv5i7tmbx9o") the url: File "C: Users User Appdata Local Programs Python Python37 lib urllib request.py", line 222, in urlopen Return opener.open(url, data, timeout) File "C: Users User Appdata Local Programs Python Python37 lib urllib request.py", line 531, in open Sponse = meth(req, Sponse) File "C: Users User Appdata Local Programs Python Python37 lib urllib request.py", line 641, in http_response http, request, sponse, 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

  • 1

    What mistake you’re making?

  • And correct your code on the question, please. The way you are you can’t even properly understand what you did.

  • Corrected colleague. Thank you.

  • You can have spaces in the URL, you could print and post here?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.