× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to decode UTF-8 url in Python

Martijn Pieters
Sep 05, 2014
<p>The data is UTF-8 encoded bytes escaped with URL quoting, so you want to <strong>decode</strong>:</p><pre><code><span style="font-size:14px">url</span><span style="font-size:14px">=</span><span style="font-size:14px">urllib</span><span style="font-size:14px">.</span><span style="font-size:14px">unquote</span><span style="font-size:14px">(</span><span style="font-size:14px">url</span><span style="font-size:14px">).</span><span style="font-size:14px">decode</span><span style="font-size:14px">(</span><span style="color:rgb(128, 0, 0); font-size:14px">'utf8'</span><span style="font-size:14px">)</span><span style="font-size:14px"> </span></code></pre><p>Demo:</p><pre><code><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">import</span><span style="font-size:14px"> urllib </span><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> url</span><span style="font-size:14px">=</span><span style="color:rgb(128, 0, 0); font-size:14px">'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'</span><span style="font-size:14px"> </span><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> urllib</span><span style="font-size:14px">.</span><span style="font-size:14px">unquote</span><span style="font-size:14px">(</span><span style="font-size:14px">url</span><span style="font-size:14px">).</span><span style="font-size:14px">decode</span><span style="font-size:14px">(</span><span style="color:rgb(128, 0, 0); font-size:14px">'utf8'</span><span style="font-size:14px">)</span><span style="font-size:14px"> u</span><span style="color:rgb(128, 0, 0); font-size:14px">'example.com?title=\u043f\u0440\u0430\u0432\u043e\u0432\u0430\u044f+\u0437\u0430\u0449\u0438\u0442\u0430'</span><span style="font-size:14px"> </span><span style="font-size:14px">&gt;&gt;&gt;</span><span style="font-size:14px"> </span><span style="color:rgb(0, 0, 139); font-size:14px">print</span><span style="font-size:14px"> urllib</span><span style="font-size:14px">.</span><span style="font-size:14px">unquote</span><span style="font-size:14px">(</span><span style="font-size:14px">url</span><span style="font-size:14px">).</span><span style="font-size:14px">decode</span><span style="font-size:14px">(</span><span style="color:rgb(128, 0, 0); font-size:14px">'utf8'</span><span style="font-size:14px">)</span><span style="font-size:14px"> example</span><span style="font-size:14px">.</span><span style="font-size:14px">com</span><span style="font-size:14px">?</span><span style="font-size:14px">title</span><span style="font-size:14px">=правовая+защита</span></code></pre><p>This tip was originally posted on <a href="http://stackoverflow.com/questions/16566069/url-decode-utf-8-in-python/16566128#16566128">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus