python convert chinese characters in url

荒凉一梦 提交于 2019-11-28 05:53:54

问题


I have a url like

href="../job/jobarea.asp?C_jobtype=經營管理主管&peoplenumber=151",

this is shown in inspect element. But when opened in new tab it is showing as

../job/jobarea.asp?C_jobtype=%B8g%C0%E7%BA%DE%B2z%A5D%BA%DE&peoplenumber=151

How do I know which type of encoding is used by the browser to convert it. When I try to do scrapy it is showing some other format and it is stopping as 500 internal server error. Could you please explain me??


回答1:


It's Tradtional Chinese, so try cp950

#-*-coding:utf8 -*-

import urllib
s = '經營管理主管'.decode('utf-8').encode('cp950')
print urllib.quote(s)

q ='%B8g%C0%E7%BA%DE%B2z%A5D%BA%DE'
print urllib.unquote(q).decode('cp950').encode('utf-8')

Result

%B8g%C0%E7%BA%DE%B2z%A5D%BA%DE
經營管理主管


来源:https://stackoverflow.com/questions/29486331/python-convert-chinese-characters-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!