mattes
mattes

Reputation: 9429

XPath: Retrieving text from a class

Given the following HTML string, parsed by lxml:

<strong class="footer"> 
  <span class="icon-new"><i class="icon-new"/></span> 
  16 
</strong>

How can I extract the number 16 with XPath?

Upvotes: 0

Views: 84

Answers (2)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

with pure Xpath

//strong[@class='footer']/normalize-space(text()[position()=last()])

Upvotes: 1

Guy Gavriely
Guy Gavriely

Reputation: 11396

as '16' text is under the strong tag, you may do it like:

>>> root = etree.fromstring(html)
>>> root.xpath('//strong[@class="footer"]/text()[normalize-space()]')[0].strip()
'16'

Upvotes: 2

Related Questions