Python 汉字转url 参数

主要是利用Python 的from urllib.request import quote,unquote 这个包,网上其他的写法好像已经过时了:

这是我宁外一种写法,但是后面用不起了,

from urllib.request import quote,unquote

X = '角色扮演'
print(X)
Y =quote(X)
print(Y)
Z = unquote(Y)
print(Z)

优化封装版本:

from urllib.request import quote,unquote
url="http://api.map.baidu.com/geocoding/v3/?address=%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA%E4%B8%8A%E5%9C%B0%E5%8D%81%E8%A1%9710%E5%8F%B7&output=json&ak=scUcZrU6RMS22mLLN7goTNX6efCbeveG&callback=showLocation"

# 汉字转url参数
class Urlquote(object):
    def url_quote(self,str):
        url_quote =quote(str)
        return url_quote

    def url_unquote(self,str):
        url_unquote=unquote(str)
        return url_unquote


if __name__ == '__main__':
    str1 = "%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA%E4%B8%8A%E5%9C%B0%E5%8D%81%E8%A1%9710%E5%8F%B7"
    url = Urlquote().url_unquote(str1)
    print(url)

猜你喜欢

转载自blog.csdn.net/weixin_44818729/article/details/109718024