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~'
'~' 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:
or you can use request module as well
Edit: As you are specific about url_encoding for all the special characters, I'm adding the following function. Use the following function: