Python3 Url 编码和解码

功能类

import urllib.parse

class UrlChuLi:
    """Url处理类,需要传入两个实参:UrlChuLi('实参','编码类型'),默认utf-8
    url编码方法:url_bm() url解码方法:url_jm()"""

    def __init__(self, can, encoding='utf-8'):
        self.can = can
        self.encoding = encoding

    def url_bm(self):
        """url_bm() 将传入的中文实参转为UrlEncode编码"""
        quma = str(self.can).encode(self.encoding)
        return urllib.parse.quote(quma)

    def url_jm(self):
        """url_jm() 将传入的url进行解码成中文"""
        quma = str(self.can)
        return urllib.parse.unquote(quma, self.encoding)

编码

encode_string = UrlChuLi("编码内容", "gbk").url_bm()
print(encode_string)

解码

decode_string = UrlChuLi("%B1%E0%C2%EB%C4%DA%C8%DD", "gbk").url_jm()
print(decode_string)

你可能感兴趣的:(Python3 Url 编码和解码)