Python解析URL字符编码

任务:在很多情况下,我们需要解析URL字符编码,如%E2%80%93代表-.
这个时候可以使用python的urllib库,参考链接。

from urllib import unquote

if __name__ == '__main__':
    s = "1961%E2%80%9362_Slovenian_Republic_League"
    print("before decoding:%s, after decoding:%s" % (s, unquote(s)))

    s = "1894_Argentine_Primera_Divisi%C3%B3n"
    print("before decoding:%s, after decoding:%s" % (s, unquote(s)))

输出结果:

before decoding:1961%E2%80%9362_Slovenian_Republic_League, after decoding:196162_Slovenian_Republic_League
before decoding:1894_Argentine_Primera_Divisi%C3%B3n, after decoding:1894_Argentine_Primera_División

反之,如果要生成URL字符编码,可以使用urllib.quote函数:

from urllib import quote
print("%s编码之后为%s" %("上海",quote("上海")))

结果输出

上海编码之后为%E4%B8%8A%E6%B5%B7

你可能感兴趣的:(python)