Python/xpath get instances of text in arbitrary element
Question: Given the following:

    
    
    <table>
        <tr>
            <td>
                <div>Text 1</div>
            </td>
            <td>
                Text 2
            </td>
            <td>
                <div>
                    <a href="#">Text 3</a>
                </div>
            </td>
        </tr>
        <tr>
            ...
        </tr>
    </table>
    

Given the above table, how would I extract all the text? Note that the number
of nested elements is arbitrary so I can't just look for the first sibling,
zero-th sibling, and second sibling.

I'm looking for a general way to extract the text.

    
    
    In [1]: d="""<table>
       ...:     <tr>
       ...:         <td>
       ...:             <div>Text 1</div>
       ...:         </td>
       ...:         <td>
       ...:             Text 2
       ...:         </td>
       ...:         <td>
       ...:             <div>
       ...:                 <a href="#">Text 3</a>
       ...:             </div>
       ...:         </td>
       ...:     </tr>
       ...:     <tr>
       ...:         ...
       ...:     </tr>
       ...: </table>"""
    
    In [3]: from lxml import etree
    
    In [4]: f = etree.HTML(d)
    
    In [5]: f.xpath('normalize-space(string(/table))')
    Out[5]: ''
    
    In [6]: f.xpath('normalize-space(string(//table))')
    Out[6]: 'Text 1 Text 2 Text 3 ...
    


Answer: I would use:

    
    
    normalize-space(string(/table))
    

