How to url encode special character ~ in Python?

136 Views Asked by At

I am trying to do an url encoding on the following string the following string 'https://bla/ble:bli~' using the urllib standard library on Python 3.10. Problem is that the ~ character is not encoded to %7E as per url encoding. Is there a way around it?

I have tried

from urllib.parse import quote

input_string = 'https://bla/ble:bli~'

url_encoded = quote(input_string)

url_encoded takes the value 'https%3A//bla/ble%3Abli~'

I have also tried:

url_encoded = quote(input_string,safe='~')

In this case the url_encoded takes the value 'https%3A%2F%2Fbla%2Fble%3Abli~'

2

There are 2 best solutions below

4
Suramuthu R On BEST ANSWER

'~' character is not required to be encoded. (Though its encoded formate is %7E). The tilde is one of the allowed characters in url. So you can use any of the followings:

from urllib.parse import quote
input_string = 'https://bla/ble:bli~'
url_encoded = quote(input_string)

or you can use request module as well

import requests
input_string = 'https://bla/ble:bli~'
url_encoded = requests.utils.requote_uri(input_string)

Edit: As you are specific about url_encoding for all the special characters, I'm adding the following function. Use the following function:

def url_encode(url):

    #add more characters into the list if required
    lst = ['[', '@', '_', '!', '#', '$', '%', '^', '&', '*', '(', ')', '<', '>', '?', '/', '\\', '|', '{', '}', '~', ':', ']]']
    for x in url:
        if x in lst:
            new = hex(ord(x)).replace('x', '').upper()
            new = f'%{new}'
            url = url.replace(x, new)
    return url
3
Stamatis Tiniakos On

I used the following solution but the one accepted is also good.

def encode_all(string):
    encoded_chars = []
    for char in string:
        if char.isalnum():  # Exclude letters and numbers
            encoded_chars.append(char)
        else:
            encoded_chars.append("%{0:0>2X}".format(ord(char)))
    return "".join(encoded_chars)