python中的URL编码和解码: test.py
1 # 引入urllib的request模块 2 import urllib.request 3 4 url = '/j/search?q=刘德华&start=20&cat=1002' 5 print('原来的url为: ' + url) 6 # url编码 safe ='/:?=&' 表示/:?=&不转义 7 url_encode = urllib.request.quote(url, safe='/:?=&', encoding='utf-8') 8 print('输出编码后的url_encode: '+ url_encode) 9 # url解码10 url_decode = urllib.request.unquote(url_encode)11 print('输出解码后的url_decode: '+ url_decode)
运行后: