urllib.parse.quote 使用 python2 实现

发布时间: 更新时间: 总字数:109 阅读时间:1m 作者: IP上海 分享 网址

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.

Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数