Decodificar caracteres escapados en URL


Tengo una lista que contiene URLs con caracteres escapados. Esos caracteres han sido establecidos por urllib2.urlopen cuando recupera la página html:

http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&action=edit
http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&action=history
http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&variant=zh 

¿Hay alguna manera de transformarlos de vuelta a su forma no escapada en python?

P.D.: Las URLs están codificadas en utf-8

Author: Björn Pollex, 2011-11-15

5 answers

Documentos oficiales.

urllib.unquote(cadena)

Reemplace %xx los escapes por su equivalente de un solo carácter.

Ejemplo: unquote('/%7Econnolly/') rinde '/~connolly/'.

Y luego simplemente decodificar.


Actualización: Para Python 3, escriba lo siguiente:

urllib.parse.unquote(url)

Python 3 docs.

 100
Author: Ignacio Vazquez-Abrams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-19 11:52:52

Y si estás usando Python3 podrías usar:

urllib.parse.unquote(url)
 21
Author: Vladir Parrado Cruz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-04 15:03:14

O urllib.unquote_plus

>>> import urllib
>>> urllib.unquote('erythrocyte+membrane+protein+1%2C+PfEMP1+%28VAR%29')
'erythrocyte+membrane+protein+1,+PfEMP1+(VAR)'
>>> urllib.unquote_plus('erythrocyte+membrane+protein+1%2C+PfEMP1+%28VAR%29')
'erythrocyte membrane protein 1, PfEMP1 (VAR)'
 9
Author: dli,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-10 04:27:02

Puede utilizar urllib.unquote

 6
Author: Klaus Byskov Pedersen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-15 13:09:14
import re

def unquote(url):
  return re.compile('%([0-9a-fA-F]{2})',re.M).sub(lambda m: chr(int(m.group(1),16)), url)
 3
Author: mistercx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-26 00:27:53