Python对字符串进行url解码后并进行unicode解码

#本文主要是对字符串进行url解码,并对解码后的内容再次进行unicode解码

url_dict={'%08':'backspace','%B2':'a','%49':'I','%76':'v','%D3':'ó','%09':'tab','%4A':'J','%77':'w','%D4':'Ô','%0A':'linefeed','%4B':'K','%78':'x','%D5':'Õ','%0D':'creturn','%4C':'L','%79':'y','%D6':'Ö','%20':'space','%4D':'M','%7A':'z','%D8':'Ø','%21':'!','%4E':'N','%7B':'{','%D9':'ù','%22':'"','%4F':'O','%7C':'|','%DA':'ú','%23':'#','%50':'P','%7D':'}','%DB':'Û','%24':'$','%51':'Q','%7E':'~','%DC':'ü','%25':'%','%52':'R','%A2':'¢','%DD':'Y','%26':'&','%53':'S','%A3':'£','%DE':'T','%27':'‘','%54':'T','%A5':'¥','%DF':'ß','%28':'(','%55':'U','%A6':'|','%E0':'à','%29':')','%56':'V','%A7':'§','%E1':'á','%2A':'*','%57':'W','%AB':'«','%E2':'a','%2B':'+','%58':'X','%AC':'¬','%E3':'ã','%2C':',','%59':'Y','%AD':'ˉ','%E4':'ä','%2D':'','%5A':'Z','%B0':'o','%E5':'å','%2E':'.','%5B':'[','%B1':'±','%E6':'æ','%2F':'/','%5C':'\\','a':'%B2','ç':'%E7','%30':'0','%5D':']','%B4':',','%E8':'è','%31':'1','%5E':'^','%B5':'μ','%E9':'é','%32':'2','%5F':'_','%33':'3','%60':'`','%34':'4','%61':'a','%35':'5','%62':'b','%36':'6','%37':'7','%63':'c','%64':'d','%38':'8','%65':'e','%F0':'ee','%39':'9','%66':'f','%3A':':','%67':'g','%3B':';','%68':'h','%3C':'<','%69':'i','%3D':'=','%6A':'j','%3E':'>','%6B':'k','%6C':'l','%F7':'÷','%40':'@','%6D':'m','%41':'A','%6E':'n','%42':'B','%6F':'o','%43':'C','%70':'p','%44':'D','%71':'q','%45':'E','%72':'r','%FD':'y','%46':'F','%73':'s','%D0':'D','%FE':'t','%47':'G','%74':'t','%48':'H','%75':'u'}

str1 = str(input('请输入要解码的url:'))

for k in url_dict.keys():

str1=str1.replace(k,url_dict[k]).encode('utf8').decode('unicode_escape')

print('结果是:',str1)

#或者为了方便起见,只对指定字符串进行url解码并进行Unicode解码

str1 = str(input('请输入要解码的url:'))

str1 = str1.replace('%5B','\\u').replace('%5D','').replace('%2F','/') #url解码-替换

print('结果是:',str1.encode('utf8').decode('unicode_escape')) #对字符串进行Unicode解码

 

你可能感兴趣的:(Python)