Url在Python中解码UTF-8 [英] Url decode UTF-8 in Python

查看:89
本文介绍了Url在Python中解码UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了很多时间,就像在Python中的新手一样。

我如何解码这样一个URL:

I have spent plenty of time as far as I am newbie in Python.
How could I ever decode such a URL:

example.com?title=%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D0%B2%D0%B0%D1%8F+%D0%B7%D0%B0%D1%89%D0%B8%D1%82%D0%B0

这一个在python 2.7中:example.com?title=\"правовая+защита

to this one in python 2.7: example.com?title==правовая+защита

url = urllib.unquote(url.encode(utf8))正在返回一个非常难看的东西。

url=urllib.unquote(url.encode("utf8")) is returning something very ugly.

仍然没有解决方案,任何帮助

Still no solution, any help is appreciated.

推荐答案

数据是使用URL引用转义的UTF-8编码字节,因此您要 strong>:

The data is UTF-8 encoded bytes escaped with URL quoting, so you want to decode:

url=urllib.unquote(url).decode('utf8') 

演示:

>>> import urllib 
>>> url='example.com?title=%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D0%B2%D0%B0%D1%8F+%D0%B7%D0%B0%D1%89%D0%B8%D1%82%D0%B0'
>>> urllib.unquote(url).decode('utf8') 
u'example.com?title=\u043f\u0440\u0430\u0432\u043e\u0432\u0430\u044f+\u0437\u0430\u0449\u0438\u0442\u0430'
>>> print urllib.unquote(url).decode('utf8')
example.com?title=правовая+защита

这篇关于Url在Python中解码UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆