前言
记录一下用python对文字的url编码和url解码方法,不对基础url编码分析
urllib库是一个python的自带库,使用的时候不需要下载,可以直接导入使用·
相关介绍
URL编码百度百科
对python中url参数编码与解码的实例详解
一、函数介绍
函数 介绍
urllib.parse.quote(字符串) 对字符串进行URL编码
urllib.parse.unquote(字符串) 对字符串进行URL解码
二、编码
在转换成URL编码后,密文的字母都为大写
使用方法:urllib.parse.quote(字符串)
# coding=utf-8
import urllib
from urllib import parse
txt = '你好'
#URL编码
new_txt = urllib.parse.quote(txt)
print(new_txt)
三、解码
大写+小写,纯小写,纯大写的形式都可被成功解码
当URL编码时,不需要变更字符串的大小写
使用方法:urllib.parse.unquote(字符串)
# coding=utf-8
import urllib
from urllib import parse
txt = '%e4%BD%A0%E5%A5%BD'
#URL解码
new_txt = urllib.parse.unquote(txt)
print(new_txt)