Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: leogtz en 1 Abril 2010, 04:48 am



Título: Busqueda en la RAE (rae.py)
Publicado por: leogtz en 1 Abril 2010, 04:48 am
Código
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author : Leo Gutiérrez Ramirez, leorocko13@hotmail.com
  4. # html2txt by Francois Granger, <fgranger@altern.org>
  5. import sys
  6. import re
  7. import urllib
  8.  
  9. def Usage():
  10. print "\nRealiza busquedas en el diccionario de la [Real Academia Española]\n";
  11. print "\t" + sys.argv[0] + " Palabra\n";
  12. sys.exit(1);
  13.  
  14. if len(sys.argv) <= 1:
  15. Usage();
  16.  
  17. url = "http://buscon.rae.es/draeI/SrvltGUIBusUsual?LEMA=" + sys.argv[1]
  18. f = urllib.urlopen(url);
  19. texto = f.read();
  20. f.close();
  21.  
  22. # Convierte el código HMTL a texto para ser mostrado correctamente en la consola:
  23.  
  24. p = re.compile('(<p.*?>)|(<tr.*?>)', re.I)
  25. t = re.compile('<td.*?>', re.I)
  26. comm = re.compile('<!--.*?-->', re.M)
  27. tags = re.compile('<.*?>', re.M)
  28.  
  29. def html2txt(texto, hint = 'entity', code = 'ISO-8859-1'):
  30.        texto = texto.replace('\n', '') # remove returns time this compare to split filter join
  31.        texto = p.sub('\n', texto) # replace p and tr by \n
  32.        texto = t.sub('\t', texto) # replace td by \t
  33.        texto = comm.sub('', texto) # remove comments
  34.        texto = tags.sub('', texto) # remove all remaining tags
  35.        texto = re.sub(' +', ' ', texto) # remove running spaces this remove the \n and \t
  36. # Handling of entities
  37. result = texto;
  38. pass;
  39. return result;
  40. print html2txt(texto);
  41. sys.exit(0);

Modo de uso:
Código:
python rae.py palabra


Título: Re: Busqueda en la RAE (rae.py)
Publicado por: Masita en 1 Abril 2010, 11:46 am
Me lo guardo para cuando quede a jugar al scrable jajaja