53
lib/data_processing.py
Normal file
53
lib/data_processing.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
def process_html(html_doc):
|
||||
"""
|
||||
Procesa el HTML de la página y extrae la información relevante
|
||||
como materias, estados del servicio social, etc.
|
||||
"""
|
||||
soup = BeautifulSoup(html_doc, 'lxml')
|
||||
table = soup.find('table', attrs={'id': 'ctl00_contenedor_HistorialAlumno1_gvMaterias'})
|
||||
|
||||
actualmente_cursadas = soup.find('table', attrs={'id': 'ctl00_contenedor_HistorialAlumno1_gvMatOrdinario'})
|
||||
|
||||
if table is None:
|
||||
raise Exception("Tabla no encontrada en la página")
|
||||
elif actualmente_cursadas is None:
|
||||
raise Exception("Tabla de materias actualmente cursadas no encontrada en la página")
|
||||
|
||||
materias_sgu = []
|
||||
headers = [header.text for header in table.find_all('th')]
|
||||
|
||||
for row in table.find_all('tr'):
|
||||
cols = row.find_all('td')
|
||||
if cols and not any(col.text == "Promedio:" for col in cols):
|
||||
materias_sgu.append({ headers[i]: col.text for i, col in enumerate(cols) if not is_cell_empty(col.text) })
|
||||
materias_actualmente_cursadas = []
|
||||
headers = [header.text for header in actualmente_cursadas.find_all('th')]
|
||||
for row in actualmente_cursadas.find_all('tr'):
|
||||
cols = row.find_all('td')
|
||||
if cols and not any(col.text == "No hay Datos" for col in cols):
|
||||
materias_actualmente_cursadas.append({ headers[i]: col.text for i, col in enumerate(cols) if not is_cell_empty(col.text) })
|
||||
|
||||
periodo_actual = soup.find('span', attrs={'id': 'ctl00_contenedor_HistorialAlumno1_Header1_lblPeriodo'})
|
||||
grupo_actual = soup.find('span', attrs={'id': 'ctl00_contenedor_HistorialAlumno1_Header1_lblgru'})
|
||||
|
||||
return clean_materias_sgu(materias_sgu), clean_materias_sgu(materias_actualmente_cursadas), f"'{periodo_actual.text}'", f"'{grupo_actual.text}'"
|
||||
|
||||
def is_cell_empty(cell_content):
|
||||
"""
|
||||
Verifica si el contenido de una celda está vacío o contiene solo espacios.
|
||||
"""
|
||||
return not cell_content.strip() or cell_content == u'\xa0'
|
||||
|
||||
def clean_materias_sgu(materias_sgu):
|
||||
"""
|
||||
Limpia y ajusta las materias obtenidas del SGU para su procesamiento posterior.
|
||||
"""
|
||||
for materia in materias_sgu:
|
||||
if '\xa0' in materia:
|
||||
materia['SEMESTRE'] = materia.pop('\xa0')
|
||||
return materias_sgu
|
||||
|
||||
# Puedes agregar más funciones según sea necesario para procesar otros aspectos del HTML
|
||||
|
||||
Reference in New Issue
Block a user