fork download
  1. # Obtener String vacio '' al buscar en xpath /text() cuando el nodo está vacío
  2. # https://es.stackoverflow.com/q/76492/127
  3.  
  4. from lxml import html
  5.  
  6. cadena = '''
  7. <body>
  8. <td><span>$3,480.00</span></td>
  9. <td><span>Ingreso</span></td>
  10. <td><span>Vigente</span></td>
  11. <td><span></span></td>
  12. <td><span>Otro</span></td>
  13. </body>
  14. '''
  15. doc = html.fromstring(cadena)
  16.  
  17.  
  18. datos = [x.xpath('string()') for x in doc.xpath('//td/span')]
  19.  
  20. print(datos)
Success #stdin #stdout 0.04s 96448KB
stdin
Standard input is empty
stdout
['$3,480.00', 'Ingreso', 'Vigente', '', 'Otro']