Search code examples
pythonweb-scrapingxpathlxml

XPath match every node containing text


How do I match all child nodes containing text recursively.

If I have a tree like

table
 tr
  td
   "hello"
  td
   b
    "hi"
 tr
  td
   "salud"
  td
   em
    "bonjour"

How do I match every single string within the table node with xpath? Something like "//table/*/text()"?


Solution

  • The XPath expression you gave was almost correct already:

    //table//text()

    will get you all text nodes within all tables in the document.