Python 解决地址栏中文编码问题

版权声明:本文为博主原创文章,转载请注明出处 浅然的专栏 https://blog.csdn.net/w_linux/article/details/83269213

一、场景简述

笔者发现在天猫搜索店铺页面,如果输入中文,在url中会进行转码,输入英文则不变,如下图

url中的将男装进行了转码,本应q=男装

具体url:https://list.tmall.com/search_product.htm?spm=a220m.1000858.1000724.7.1f281602JoXnuH&q=%C4%D0%D7%B0&sort=s&style=w&from=..pc_1_searchbutton&active=2#J_Filter

所以笔者需要在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))

结果

好了,问题解决

猜你喜欢

转载自blog.csdn.net/w_linux/article/details/83269213