Diferencia entre revisiones de «LSWC scraping the web/taller scraping lwsc 2011/horario lwsc»

De WikiEducator
Saltar a: navegación, buscar
m
Línea 13: Línea 13:
  
 
for d in dias:
 
for d in dias:
print d.summary
+
    print d.summary
  
 
for charla in doc.xml_select(u'//td[@title][@class="python"]'):
 
for charla in doc.xml_select(u'//td[@title][@class="python"]'):
print  charla.title, ' - ', charla
+
    print  charla.title, ' - ', charla
  
 
# Podemos hacer lo mismo con menos xpath y más python ...
 
# Podemos hacer lo mismo con menos xpath y más python ...
 
for charla in doc.xml_select(u'//td'):
 
for charla in doc.xml_select(u'//td'):
if charla.title and charla['class'] == 'python':
+
    if charla.title and charla['class'] == 'python':
print charla.title, '-->>', charla
+
        print charla.title, '-->>', charla
  
 
# ¿A qué día pertenece una charla que hemos seleccionado?
 
# ¿A qué día pertenece una charla que hemos seleccionado?
 
tabla = charla.xml_select(u'ancestor::table')[0]
 
tabla = charla.xml_select(u'ancestor::table')[0]
 
if '10' in tabla.summary:
 
if '10' in tabla.summary:
print 'Esta actividad es el jueves'
+
    print 'Esta actividad es el jueves'
 
</source>
 
</source>
  
Línea 42: Línea 42:
  
 
for j in joomlas:
 
for j in joomlas:
for ch in j.xml_children:
+
    for ch in j.xml_children:
j.xml_remove(ch)
+
        j.xml_remove(ch)
j.xml_append(text(u'&nbsp;'))
+
    j.xml_append(text(u'&nbsp;'))
  
 
open('nuevo_horario.html', 'w').write(doc.xml_encode(HTML_W))
 
open('nuevo_horario.html', 'w').write(doc.xml_encode(HTML_W))
 
</source>
 
</source>

Revisión de 11:46 10 nov 2011


Búsquedas en el documento

# -*- coding: utf-8 -*-
 
from amara.bindery import html
 
doc = html.parse('horario.html')
 
# Los días están marcados en tablas que tienen un atributo summary
dias = doc.xml_select(u'//table[@summary]')
 
for d in dias:
    print d.summary
 
for charla in doc.xml_select(u'//td[@title][@class="python"]'):
    print  charla.title, ' - ', charla
 
# Podemos hacer lo mismo con menos xpath y más python ...
for charla in doc.xml_select(u'//td'):
    if charla.title and charla['class'] == 'python':
        print charla.title, '-->>', charla
 
# ¿A qué día pertenece una charla que hemos seleccionado?
tabla = charla.xml_select(u'ancestor::table')[0]
if '10' in tabla.summary:
    print 'Esta actividad es el jueves'


Transformación del documento

Vamos a borrar a Joomla del calendario

from amara.bindery import html
from amara.writers import lookup
from amara.tree import text
HTML_W = lookup("html")  
 
doc = html.parse('horario.html')
joomlas = doc.xml_select(u'//td[@class="joomla"]')
 
for j in joomlas:
    for ch in j.xml_children:
        j.xml_remove(ch)
    j.xml_append(text(u'&nbsp;'))
 
open('nuevo_horario.html', 'w').write(doc.xml_encode(HTML_W))