Python: Importando urllib.citar


Me gustaría usar urllib.quote(). Pero python (python3) no está encontrando el módulo. Supongamos que tengo esta línea de código:

print(urllib.quote("châteu", safe=''))

Cómo puedo importar urllib.¿cita?

import urllib o import urllib.quote ambos dan

AttributeError: 'module' object has no attribute 'quote'

Lo que me confunde es que urllib.request es accesible a través de import urllib.request

Author: Drunken Master, 2015-08-05

3 answers

En Python 3.x, necesitas importar urllib.parse.quote:

>>> import urllib.parse
>>> urllib.parse.quote("châteu", safe='')
'ch%C3%A2teu'

De acuerdo con Python 2.x urllib documentación del módulo :

NOTA

El módulo urllib ha sido dividido en partes y renombrado en Python 3 para urllib.request, urllib.parse, y urllib.error.

 90
Author: falsetru,
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-08-05 08:24:38

Si necesita manejar ambos Python 2.x y 3.x puede capturar la excepción y cargar la alternativa.

try:
    from urllib import quote  # Python 2.X
except ImportError:
    from urllib.parse import quote  # Python 3+

También puedes usar el wrapper de compatibilidad de python six para manejar esto.

from six.moves.urllib.parse import quote
 26
Author: eandersson,
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
2017-04-01 22:56:51

Urllib pasó por algunos cambios en Python3 y ahora se puede importar desde el submódulo de análisis

>>> from urllib.parse import quote  
>>> quote('"')                      
'%22'                               
 9
Author: justinfay,
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-08-05 08:22:45