Экранирование url в python

How to encode URLs in Python

How to encode URLs in Python

URL encoding is often needed when you’re calling a remote api with additional query strings or path parameters. Any query string or path parameter placed in the URL must be properly URL encoded.

URL encoding is also required while preparing data for submission with application/x-www-form-urlencoded MIME type.

In this article, you’ll learn how to encode URL components in Python. Let’s get started!

URL Encoding query strings or form parameters in Python (3+)

In Python 3+, You can URL encode any string using the quote() function provided by urllib.parse package. The quote() function by default uses UTF-8 encoding scheme.

>>> import urllib.parse >>> query = 'Hellö Wörld@Python' >>> urllib.parse.quote(query) 'Hell%C3%B6%20W%C3%B6rld%40Python'

Note that, the quote() function considers / character safe by default. That means, It doesn’t encode / character

The quote() function accepts a named parameter called safe whose default value is / . If you want to encode / character as well, then you can do so by supplying an empty string in the safe parameter like this-

Encoding space characters to plus sign ( + ) using quote_plus() function

The quote() function encodes space characters to %20 . If you want to encode space characters to plus sign ( + ), then you can use another function named quote_plus provided by urllib.parse package.

>>> import urllib.parse >>> query = 'Hellö Wörld@Python' >>> urllib.parse.quote_plus(query) 'Hell%C3%B6+W%C3%B6rld%40Python'

Encoding multiple parameters at once

You can encode multiple parameters at once using urllib.parse.urlencode() function. This is a convenience function which takes a dictionary of key value pairs or a sequence of two-element tuples and uses the quote_plus() function to encode every value. The resulting string is a series of key=value pairs separated by & character.

>>> import urllib.parse >>> params = 'q': 'Python URL encoding', 'as_sitesearch': 'www.urlencoder.io'> >>> urllib.parse.urlencode(params) 'q=Python+URL+encoding&as_sitesearch=www.urlencoder.io'

If you want the urlencode() function to use the quote() function for encoding parameters, then you can do so like this —

urllib.parse.urlencode(params, quote_via=urllib.parse.quote)

Encoding multiple parameters at once where one parameter can have multiple values

The urlencode() function takes an optional argument called doseq . If your input can have multiple values for a single key, then you should set the doseq argument to True so that all the values are encoded properly —

>>> import urllib.parse >>> params = 'name': 'Rajeev Singh', 'phone': ['+919999999999', '+628888888888']> >>> urllib.parse.urlencode(params, doseq=True) 'name=Rajeev+Singh&phone=%2B919999999999&phone=%2B628888888888'

URL Encoding in Python 2.x

In Python 2.x the quote() , quote_plus() , and urlencode() functions can be accessed directly from the urllib package. These functions were refactored into urllib.parse package in Python 3.

The following examples demonstrate how you can perform URL encoding in Python 2.x using the above functions.

>>> import urllib >>> urllib.quote('Hello World@Python2') 'Hello%20World%40Python2'
>>> import urllib >>> urllib.quote_plus('Hello World@Python2') 'Hello+World%40Python2'
>>> import urllib >>> params = 'q': 'Python 2.x URL encoding', 'as_sitesearch': 'www.urlencoder.io'> >>> urllib.urlencode(params) 'q=Python+2.x+URL+encoding&as_sitesearch=www.urlencoder.io'

Источник

Читайте также:  Внешняя таблица стилей
Оцените статью