python3.5 urllib.parse.unquote garbled code
高洛峰
高洛峰 2017-05-18 10:57:00
0
3
2254

use

urllib.parse.unquote(from_ur)

decoding


%E7%8E%8B%E7%90%BC%E8%8C%B6%E8%89%BA
%E5%B0%8F%E7%BD%90%E8%8C%B6%E8%83%8C%E6%99%AF%E6%AD%8C%E6%9B%B2
%C2%BD%D3%F0%CD%BC%C6%AC
%E5%B0%8F%E7%BD%90%E8%8C%B6%E8%83%8C%E6%99%AF%E9%9F%B3%E4%B9%90
%C2%CC%B2%E8 
%C2%CC%B2%E8%CD%BC%C6%AC%B4%F3%C8%AB   
%BB%B1%BB%A8%B2%E8
%E7%8E%8B%E7%90%BC%E8%A1%8C%E8%8C%B6%E5%8D%81%E5%BC%8F
%E5%B0%8F%E7%BD%90%E8%8C%B6%E7%9A%84%E5%85%AB%E4%B8%AA%E5%A4%A7%E5%B8%88
%E9%BE%99%E4%BA%95%E8%8C%B6%E5%A5%BD%E5%9D%8F%E5%9B%BE%E7%89%87
%E9%9B%80%E8%88%8C%E8%8C%B6%E5%8F%B6%E5%B1%9E%E4%BA%8E%E4%BB%80%E4%B9%88%E8%8C%B6
%E7%9B%96%E7%A2%97%E8%8C%B6%E6%AD%A3%E7%A1%AE%E7%9A%84%E5%96%9D%E6%B3%95%E5%9B%BE%E8%A7%A3
%E7%A2%A7%E6%BD%AD%E9%A3%98%E9%9B%AA%E7%9A%84%E5%8A%9F%E6%95%88%E4%B8%8E%E4%BD%9C%E7%94%A8
%E8%8C%B6%E9%81%93
%BA%DA%BF%E0%DC%F1
%E8%8C%B6%E7%9A%84%E5%B9%BF%E5%91%8A%E6%AD%8C%E6%9B%B2
%E4%BF%A1%E9%98%B3%E6%AF%9B%E5%B0%96
%E5%AE%89%E5%90%89%E7%99%BD%E8%8C%B6%E5%9B%BE%E7%89%87
%E6%80%8E%E6%A0%B7%E5%8A%A0%E7%9B%9F%E5%B0%8F%E7%BD%90%E8%8C%B6
%E5%AE%89%E6%BA%AA%E8%8C%B6%E5%8F%B6%E6%89%B9%E5%8F%91%E5%B8%82%E5%9C%BA
%E7%A2%A7%E8%9E%BA%E6%98%A5
%E8%8B%A6%E8%8D%9E
%BA%DA%BF%E0%DC%F1%B2%E8

Some garbled characters

Is there any good solution

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
世界只因有你
>>> unquote(s, encoding="utf-8")
'王琼茶艺\n小罐茶背景歌曲\n½��ͼƬ\n小罐茶背景音乐\n�̲�\n�̲�ͼƬ��ȫ\n������\n王琼行茶十式\n小罐茶的八个大师\n龙井茶好坏图片\n雀舌茶叶属于什么茶\n盖碗茶正确的喝法图解\n碧潭飘雪的功效与作用\n茶道\n�ڿ���\n茶的广告歌曲\n信阳毛尖\n安吉白茶图片\n怎样加盟小罐茶\n安溪茶叶批发市场\n碧螺春\n苦荞\n�ڿ����'
>>> unquote(s, encoding="gbk")
'鐜嬬惣鑼惰壓\n灏忕綈鑼惰儗鏅�姝屾洸\n陆羽图片\n灏忕綈鑼惰儗鏅�闊充箰\n绿茶\n绿茶图片大全\n槐花茶\n鐜嬬惣琛岃尪鍗佸紡\n灏忕綈鑼剁殑鍏�涓�澶у笀\n榫欎簳鑼跺ソ鍧忓浘鐗�\n闆�鑸岃尪鍙跺睘浜庝粈涔堣尪\n鐩栫�楄尪姝g‘鐨勫枬娉曞浘瑙�\n纰ф江椋橀洩鐨勫姛鏁堜笌浣滅敤\n鑼堕亾\n黑苦荞\n鑼剁殑骞垮憡姝屾洸\n淇¢槼姣涘皷\n瀹夊悏鐧借尪鍥剧墖\n鎬庢牱鍔犵洘灏忕綈鑼�\n瀹夋邯鑼跺彾鎵瑰彂甯傚満\n纰ц灪鏄�\n鑻﹁崬\n黑苦荞茶'
>>> 

As you can see, the garbled part is encoded as gbk. I don’t know why the two encodings are mixed.

Solution:



s = '''
%E7%8E%8B%E7%90%BC%E8%8C%B6%E8%89%BA
%E5%B0%8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parse import unquote_to_bytes

_list = unquote_to_bytes(s).split(b'\n')

result = []

for line in _list:
    try :
        result.append(line.decode('utf-8'))
        
    except UnicodeDecodeError:
        result.append(line.decode('gbk'))
        
print("\n".join(result))

Run results

=================== RESTART: C:/Users/Zhang/Desktop/tmp.py ===================

王琼茶艺
小罐茶背景歌曲
陆羽图片
小罐茶背景音乐
绿茶 
绿茶图片大全   
槐花茶
王琼行茶十式
小罐茶的八个大师
龙井茶好坏图片
雀舌茶叶属于什么茶
盖碗茶正确的喝法图解
碧潭飘雪的功效与作用
茶道
黑苦荞
茶的广告歌曲
信阳毛尖
安吉白茶图片
怎样加盟小罐茶
安溪茶叶批发市场
碧螺春
苦荞
黑苦荞茶

>>> 
洪涛

It is recommended to try using requests

世界只因有你

There is a problem with input. Try to find an online URL decoding page. You can see that there are also garbled characters
http://www.convertstring.com/...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!