一、场景简述
笔者发现在天猫搜索店铺页面,如果输入中文,在url中会进行转码,输入英文则不变,如下图
url中的将男装进行了转码,本应q=男装

所以笔者需要在python3.6中将中文字符进行编码,使其匹配其url
二、解决方案
type = ['女装', '男装', '计算机书籍', '电脑']
for index in type:
#解决地址栏中 中文编码问题
typename = urllib.parse.quote(index)
logging.info('type {}'.format(typename))
#解码
retypename = urllib.parse.unquote(typename)
logging.info('retype {}'.format(retypename))
结果

好了,问题解决
本文介绍在Python中如何处理中文字符的URL编码问题,通过使用urllib.parse.quote()和urllib.parse.unquote()方法,实现中文字符在URL中的正确编码与解码。
759





