一、依赖安装

pip install urllib

 二、URL编码

from urllib.parse import quote

url = r'https://myshop.com/shop/shopList?query='
query = '{"id":14,"pageSize":10,"pageNum":1}'
encoded_url = url+quote( query )
print( encoded_url )

可以找个 urlencode 在线网站 进行验证:

单引号内的部分是一样的。

Python URL编码-LMLPHP

Python URL编码-LMLPHP

三、URL解码

from urllib.parse import unquote

encoded_url= '%7B%22id%22%3A14%2C%22pageSize%22%3A10%2C%22pageNum%22%3A1%7D'
decoded_url = unquote( encoded_url )
print( decoded_url )

Python URL编码-LMLPHP

10-25 07:51