Is it possible for xpath to return NULL if there is no text data? -
i trying extract data table. table data rows formatted <td headers="h1" align="left"></td> when there no data.
using etree.tostring() method lxml library prints out these elements <td headers="h1" align="left"/> instead of source formatting.
furthermore, using xpath if run code tree.path('//td[@headers="h1"]/text()') resulting list not include blank values there no data.
as trying write these results csv file, how include null, i.e. "" when there no data?
one workaround use //td[@headers="h1"] xpath elements , .text property on each:
from lxml import etree data = """ <table> <tr> <td headers="h1" align="left"></td> <td headers="h1" align="left">text1</td> <td headers="h1" align="left"/> <td headers="h1" align="left">text2</td> <td headers="h1" align="left"></td> </tr> </table> """ tree = etree.fromstring(data) print [element.text element in tree.xpath('//td[@headers="h1"]')] prints:
[none, 'text1', none, 'text2', none] if want empty string instead of none:
print [element.text if element.text not none else '' element in tree.xpath('//td[@headers="h1"]')] would print:
['', 'text1', '', 'text2', '']
Comments
Post a Comment