url编码中文互相转换
1.python脚本中文汉字转url代码:
from urllib.parse import quote import string url = 'www.秀一下.com' url = quote(url, safe = string.printable).lower() # safe 表示可以忽略的字符,数字大小写字母特殊字符等 print(url) # %e6%88%90%e8%af%ad%e5%a4%a7%e5%85%a8
2.python代码实现url转中文汉字:
from urllib.parse import unquote import string urlud = 'www.%E7%A4%BA%E4%BE%8B.com' urlud = unquote(urlud, encoding='utf-8') # encoding 表示编码,可为 GBK 或 utf-8 print(urlud) # www.示例.com
运行测试:
完整代码:
from urllib.parse import quote from urllib.parse import unquote import string url = 'www.秀一下.com' url = quote(url, safe = string.printable).lower() # safe 表示可以忽略的字符,数字大小写字母特殊字符等 print(url) # %e6%88%90%e8%af%ad%e5%a4%a7%e5%85%a8 urlud = 'www.%E7%A4%BA%E4%BE%8B.com' urlud = unquote(urlud, encoding='utf-8') # encoding 表示编码,可为 GBK 或 utf-8 print(urlud) # www.示例.com