A blast from the past! 😄
In Python 2, the equivalent of urllib.parse.quote
is urllib.quote
.
Here’s an example:
import urllib
url = "https://example.com/search?q=hello world"
quoted_url = urllib.quote(url)
print(quoted_url) # Output: https%3A//example.com/search%3Fq%3Dhello%20world
Note that urllib.quote
will URL-encode the entire string, including the scheme, hostname, and path.
If you only want to URL-encode a specific part of the URL, you can use urllib.quote_plus
instead:
import urllib
query_string = "hello world"
quoted_query_string = urllib.quote_plus(query_string)
print(quoted_query_string) # Output: hello+world
In this case, urllib.quote_plus
will URL-encode the query string, replacing spaces with +
characters.
Keep in mind that urllib.quote
and urllib.quote_plus
are deprecated in Python 3.x, and you should use urllib.parse.quote
and urllib.parse.quote_plus
instead.