lxml classic: Get text content except for that of nested tags?

后端 未结 2 1091
无人及你
无人及你 2021-01-14 15:49

This must be an absolute classic, but I can\'t find the answer here. I\'m parsing the following tag with lxml cssselect:

  • 2条回答
    •  日久生厌
      2021-01-14 16:29

      For your example, I think going with XPath is cleaner and easier than CSS:

      >>> xml = '
    • 3 Detroit
    • ' >>> root = etree.fromstring(xml) >>> print( root.xpath('/li/a/text()')) [' Detroit'] >>> xml = '
    • I FooBar! love 3 Detroit
    • ' >>> root = etree.fromstring(xml) >>> print( root.xpath('/li/a/text()')) ['I ', ' love ', ' Detroit'] >>> ' '.join([x.strip() for x in root.xpath('/li/a/text()')]) 'I love Detroit'

    提交回复
    热议问题