Thread View: es.comp.os.linux.programacion
4 messages
4 total messages
Started by pspbar
Sun, 10 May 2009 12:09
python, beautifulsoup y utf
Author: pspbar
Date: Sun, 10 May 2009 12:09
Date: Sun, 10 May 2009 12:09
42 lines
2067 bytes
2067 bytes
Hola
tengo un script en python y beautifulsoup con el que accedo a una
página web e imprimo en pantalla
unos cuantos datos, todo muy sencillo.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
from BeautifulSoup import BeautifulSoup
page = urllib2.urlopen("http://www.onlae.es/primitiva/comprobar.aspx")
soup = BeautifulSoup(page)
page.close()
(...)
# Fecha del sorteo
sorteo = soup.find("h2", {"class" : "sorteo"})
print sorteo.string
(...)
Desde la consola, al ejecutar el script todo funciona sin problemas:
~$ tmp/loto.py
Sorteo
19/S, sábado, 09 de mayo de 2009
(...)
Pero si el script se ejecuta desde crontab, me da un error de
codificación:
Traceback (most recent call last):
File "/home/user/tmp/loto.py", line 25, in <module>
print sorteo.string
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in
position 31: ordinal not in range(128)
¿Alguna idea de como solucionar esto?
Re: python, beautifulsoup y utf
Author: Mario Teijeiro Otero
Date: Sun, 10 May 2009 12:23
Date: Sun, 10 May 2009 12:23
50 lines
2196 bytes
2196 bytes
pspbar@gmail.com wrote:
Me da que no están bien definidas las variables LC_XX en la tarea del cron.
Intenta definirlas al crear la tarea del crontab.
Un saludo
> Hola
>
> tengo un script en python y beautifulsoup con el que accedo a una
> página web e imprimo en pantalla
> unos cuantos datos, todo muy sencillo.
>
> #! /usr/bin/env python
> # -*- coding: utf-8 -*-
> import urllib2
> from BeautifulSoup import BeautifulSoup
>
> page = urllib2.urlopen("http://www.onlae.es/primitiva/comprobar.aspx")
> soup = BeautifulSoup(page)
> page.close()
>
> (...)
>
> # Fecha del sorteo
> sorteo = soup.find("h2", {"class" : "sorteo"})
> print sorteo.string
>
> (...)
>
> Desde la consola, al ejecutar el script todo funciona sin problemas:
>
> ~$ tmp/loto.py
>
> Sorteo
>
> 19/S, sábado, 09 de mayo de 2009
> (...)
>
> Pero si el script se ejecuta desde crontab, me da un error de
> codificación:
>
> Traceback (most recent call last):
> File "/home/user/tmp/loto.py", line 25, in <module>
> print sorteo.string
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in
> position 31: ordinal not in range(128)
>
> ¿Alguna idea de como solucionar esto?
Re: python, beautifulsoup y utf
Author: pspbar
Date: Sun, 10 May 2009 13:40
Date: Sun, 10 May 2009 13:40
10 lines
1578 bytes
1578 bytes
On 10 mayo, 14:23, Mario Teijeiro Otero <eme...@escomposlinux.org> wrote: ~$ crontab -l LANG=es_ES.UTF-8 LC_MESSAGES=es_ES.UTF-8 LC_TYPE=es_ES.UTF-8 # m h dom mon dow command 37 15 * * 0 /home/user/tmp/loto.py mismo error.
Re: python, beautifulsoup y utf
Author: Samuel L. Jackson
Date: Thu, 04 Jun 2009 22:39
Date: Thu, 04 Jun 2009 22:39
24 lines
2089 bytes
2089 bytes
pspbar@gmail.com <pspbar@gmail.com> dijo:
> On 10 mayo, 14:23, Mario Teijeiro Otero <eme...@escomposlinux.org>
> wrote:
> ~$ crontab -l
> LANG=es_ES.UTF-8
> (...)
> mismo error.
Nada que ver con la variable LANG, sino con el propio python:
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
Solución: editar /usr/lib/python/sitecustomize.py y añadir las líneas
import sys
sys.setdefaultencondig("utf-8")
La explicación en google, buscando "the illusive setdefaultencoding"
(no tengo la url ahora mismo, ni conexion mientras escribo esto)
--
Lorem ipsum dolor sit amet
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads