Python中文编码,只针对url内部中文编码处理问题,,已经我的url为:htt


已经我的url为:http://map.baidu.com/?newmap=1&s=con%26wd%3D阿坝师范高等专科学校%26c%3D185&from=alamap&tpl=mapdots

但是使用urllib中的quote方法编码后的结果为:http%3A//map.baidu.com/%3Fnewmap%3D1%26s%3Dcon%2526wd%253D%E9%98%BF%E5%9D%9D%E5%B8%88%E8%8C%83%E9%AB%98%E7%AD%89%E4%B8%93%E7%A7%91%E5%AD%A6%E6%A0%A1%2526c%253D185%26from%3Dalamap%26tpl%3Dmapdots

即:quote对其他字符也进行了编码。

问:如何只对url中的中文编码?

先unquote再quote就好了。

import urllibprint urllib.quote_plus( urllib.unquote_plus( "http://map.baidu.com/?newmap=1&s=con%26wd%3D阿坝师范高等专科学校%26c%3D185&from=alamap&tpl=mapdots" ) )
>>> from urllib.parse import *>>> url = 'http://map.baidu.com/?newmap=1&s=con%26wd%3D阿坝师范高等专科学校%26c%3D185&from=alamap&tpl=mapdots'>>> import string>>> quote(url, safe=string.printable)                                                                                                                  'http://map.baidu.com/?newmap=1&s=con%26wd%3D%E9%98%BF%E5%9D%9D%E5%B8%88%E8%8C%83%E9%AB%98%E7%AD%89%E4%B8%93%E7%A7%91%E5%AD%A6%E6%A0%A1%26c%3D185&from=alamap&tpl=mapdots'

编橙之家文章,

评论关闭