Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to url encode special character ~ in Python?

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~'

like image 605
Stamatis Tiniakos Avatar asked Dec 30 '25 21:12

Stamatis Tiniakos


1 Answers

'~' 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
like image 182
Suramuthu R Avatar answered Jan 02 '26 11:01

Suramuthu R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!