Python urllib encode url

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'

Источник

How to url encode in python 3?

URL encoding is a method of converting special characters in a URL into a format that can be transmitted over the internet. In Python, the process of URL encoding can be done using the urllib.parse module, which contains several functions for working with URLs. In this article, we will discuss the various methods available for URL encoding in Python 3.

Method 1: urllib.parse.quote()

To URL encode a string in Python 3, you can use the urllib.parse.quote() function. This function takes a string as input and returns a URL-encoded string.

Here’s an example of how to use urllib.parse.quote() :

import urllib.parse url = 'https://www.example.com/search?q=python tutorial' encoded_url = urllib.parse.quote(url) print(encoded_url)
https%3A//www.example.com/search%3Fq%3Dpython%20tutorial

In this example, we first import the urllib.parse module which contains the quote() function. We then define a URL string with a query parameter. We pass this URL string to the quote() function to get the URL-encoded string. Finally, we print the encoded URL string.

The quote() function can also take an optional safe parameter which specifies additional characters that should not be encoded. For example, if you want to encode a URL string but leave the forward slashes ( / ) unencoded, you can pass the safe=’/’ parameter:

import urllib.parse url = 'https://www.example.com/search?q=python tutorial' encoded_url = urllib.parse.quote(url, safe='/') print(encoded_url)
https://www.example.com/search?q=python%20tutorial

In this example, we pass the safe=’/’ parameter to the quote() function. This tells the function to leave the forward slashes unencoded.

That’s it! With urllib.parse.quote() , you can easily URL encode strings in Python 3.

Method 2: urllib.parse.quote_plus()

To URL encode in Python 3 using urllib.parse.quote_plus() , follow these steps:

encoded_string = urllib.parse.quote_plus("Hello, world!")

The quote_plus() function replaces special characters in the string with their URL-encoded equivalents. For example, the space character is replaced with %20 .

import urllib.parse original_string = "Hello, world!" encoded_string = urllib.parse.quote_plus(original_string) print("Original string:", original_string) print("Encoded string:", encoded_string)
Original string: Hello, world! Encoded string: Hello%2C+world%21

In the example above, the original string is «Hello, world!» , and the encoded string is «Hello%2C+world%21» . The %2C represents the URL-encoded comma character, and %21 represents the URL-encoded exclamation mark character.

You can also pass additional arguments to the quote_plus() function to customize its behavior. For example, you can specify a different character encoding:

import urllib.parse original_string = "Hello, world!" encoded_string = urllib.parse.quote_plus(original_string, encoding="utf-8") print("Original string:", original_string) print("Encoded string:", encoded_string)
Original string: Hello, world! Encoded string: Hello%2C+world%21

In this example, we specified the encoding argument as «utf-8» , which is the default encoding used by quote_plus() .

Method 3: urllib.parse.urlencode()

To URL encode in Python 3 with urllib.parse.urlencode() , you can follow these steps:

from urllib.parse import urlencode
params = 'param1': 'value1', 'param2': 'value2'>
encoded_params = urlencode(params)

Here’s another example with more parameters:

params = 'name': 'John Doe', 'age': 30, 'city': 'New York'> encoded_params = urlencode(params) print(encoded_params)
name=John+Doe&age=30&city=New+York

Note that the urlencode function automatically encodes special characters such as spaces ( ) to + and non-ASCII characters to their corresponding percent-encoded values.

You can also pass the doseq parameter as True to encode multiple values for the same key as separate parameters:

params = 'param1': ['value1', 'value2'], 'param2': 'value3'> encoded_params = urlencode(params, doseq=True) print(encoded_params)
param1=value1¶m1=value2¶m2=value3

That’s it! With urllib.parse.urlencode() , you can easily encode parameters for URLs in Python 3.

Method 4: using the percent-encoding in string format

To URL encode in Python 3 using the percent-encoding in string format, you can use the urllib.parse module. Here are the steps:

string_to_encode = "Hello, world!"
encoded_string = urllib.parse.quote(string_to_encode)
import urllib.parse string_to_encode = "This is a test string with spaces and special characters: !@#$%^&*()_+-=[]<>|;':\",./<>?" encoded_string = urllib.parse.quote(string_to_encode) print(encoded_string)
This%20is%20a%20test%20string%20with%20spaces%20and%20special%20characters%3A%20%21%40%23%24%25%5E%26%2A%28%29_%2B-%3D%5B%5D%7B%7D%7C%3B%27%3A%22%2C.%2F%3C%3E%3F

That’s it! You can use this method to URL encode any string in Python 3.

Источник

How To Encode URL and Query String In Python?

How To Encode URL and Query String In Python?

URL or Uniform Resource Locator is a technology and standard used to define different digital resources. The URL is well known with the website addresses like https://www.pythontect.com. Python provides different methods in order to encode the URL properly in order to form it in a global form. The urllib.parse module can be used to encode URL and Query String. This module can parse the following protocols.

file , ftp , gopher , hdl , http , https , imap , mailto , mms , news , nntp , prospero , rsync , rtsp , rtspu , sftp , shttp , sip , sips , snews , svn , svn+ssh , telnet , wais , ws , wss

Python URL Encode Methods

Python provides the following methods in order to encode a URL or query string.

Encode URL with quote() Method

Python provides the quote() method which will encode the provided string. The quote() method is provided by the urllib.parse module which is a built-in module. There is no need to install this module just importing it will work.

import urllib.parse URL = "https://www.pythontect.com/about/?name=ismail&pass=123" encoded_URL = urllib.parse.quote(URL) print(encoded_URL)

The encoded URL which will be printed to the standard output will be like below.

https%3A//www.pythontect.com/about/%3Fname%3Dismail%26pass%3D123

As we can see from the encoded URL some signs like / do not encode into different characters. But there is a way that encodes the URL safer by encoding all non-alphabet characters. The safe parameter can be set empty string which will convert all non-alphabet characters.

import urllib.parse URL = "https://www.pythontect.com/about/?name=ismail&pass=123" encoded_URL = urllib.parse.quote(URL,safe="") print(encoded_URL)

The output will be like below.

https%3A%2F%2Fwww.pythontect.com%2Fabout%2F%3Fname%3Dismail%26pass%3D123

Encode URL or Query String with urlencode() Method

Another method to encode a URL is the urlencode() method which is provided via urllib.parse module too. As a built-in module just importing the urllib.parse module will work without problem. URL encoding is especially used for encoding query strings where they consist of key-value pairs which are very same as the dictionary items. The parameters are provided as a dictionary to the urlencode() method.

import urllib.parse query_string = < "username":"ismail baydan" , "password":"123">encoded_URL = urllib.parse.urlencode( query_string ) print(encoded_URL)

The output will be like below.

username=ismail+baydan&password=123

Encode Space As + Sign

By default the urllib.parse() method encodes the spaces as %20 . This is the most popular method to encode space. But alternatively, space can be encoded as + sign by using the urllib.parse.quote_plus() method like below.

import urllib.parse URL = "https://www.pythontect.com/about/?name=ismail baydan&pass=123" encoded_URL = urllib.parse.quote_plus(URL,safe="") print(encoded_URL)

The output is like below where the space between “ismail” and “baydan” is enoded as “+”.

https%3A%2F%2Fwww.pythontect.com%2Fabout%2F%3Fname%3Dismail+baydan%26pass%3D123

Источник

Читайте также:  Оформление личного кабинета html
Оцените статью