103

I have tried to follow the documentation but was not able to use urlparse.parse.quote_plus() in Python 3:

from urllib.parse import urlparse

params = urlparse.parse.quote_plus({'username': 'administrator', 'password': 'xyz'})

I get

AttributeError: 'function' object has no attribute 'parse'

3 Answers 3

140

You misread the documentation. You need to do two things:

  1. Quote each key and value from your dictionary, and
  2. Encode those into a URL

Luckily urllib.parse.urlencode does both those things in a single step, and that's the function you should be using.

from urllib.parse import urlencode, quote_plus

payload = {'username':'administrator', 'password':'xyz'}
result = urlencode(payload, quote_via=quote_plus)
# 'password=xyz&username=administrator'
Sign up to request clarification or add additional context in comments.

2 Comments

It's a pity that it does not work with plain strings like PHP's function urlencode(). So one must have key:value pairs, which IMHO is too restrictive. For instance, I have a need to URL encode only a part of string - password in proto://user:[email protected] cmd line to run duplicity backup. Python2 does work as intended though: python2 -c "import urllib as ul; print ul.quote_plus('$KEY');" -> where $KEY is supplied from bash script.
@stamster quote_plus is available in Python 3 the same way. python3 -c "import urllib.parse as ul; print(ul.quote_plus('$KEY'))"
105

For Python 3 you could try using quote instead of quote_plus:

import urllib.parse

print(urllib.parse.quote("http://www.sample.com/", safe=""))

Result:

http%3A%2F%2Fwww.sample.com%2F

Or:

from requests.utils import requote_uri
requote_uri("http://www.sample.com/?id=123 abc")

Result:

'https://www.sample.com/?id=123%20abc'

6 Comments

This is not Python 3 code. Unfortunately there is no quote in urllib in python3 anymore.
Try urllib.parse.quote [Edited the original post] @NiallFarrington
from requests.utils import requote_uri worked on Python 3.6
print(urllib.parse.quote("http://www.sample.com/")) prints http%3A//www.sample.com/
@AliKaraca If you want to URL encode slashes (/) as well, you have to pass safe="" as second parameter to the quote function. Like so: urllib.parse.quote("http://www.sample.com/", safe="")
|
18

You’re looking for urllib.parse.urlencode

import urllib.parse

params = {'username': 'administrator', 'password': 'xyz'}
encoded = urllib.parse.urlencode(params)
# Returns: 'username=administrator&password=xyz'

2 Comments

This does not work for say username and password in http://username:[email protected]/
Your example uses path params, not query params. For those you need: ` from urllib.parse import quote resource_path = "http://{username}:{password}@www.site.com/" path_params = dict(username='hit there', password='hi there') for k, v in path_params.items(): # specified safe chars, encode everything resource_path = resource_path.replace( '{%s}' % k, quote(str(v)) ) `

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.