Claz
Claz

Reputation: 31

Python: xpath to grab text from element in span

I have the following HTML:

<span class="time" data-time="2014-11-06T22:00:00.000+0000">10'</span>

And I am getting to it with python code like this:

import requests
from lxml import html

page=requests.get('http://www.url.com/data')
tree=html.fromstring(page.text)
empty=tree.xpath('//*[@id="stuff"]/div[1]/div[6]/div[1]/div/div/div/div[3]/span/text()')

The trouble that I am having is that this returns an empty list for this particular element. The element is a timer which is dynamically updated on the site, and it doesn't look like the text() call is doing anything.

Any help is appreciated!

Upvotes: 1

Views: 2400

Answers (1)

Claz
Claz

Reputation: 31

Got it: added an extra '/' before the text() call. So it read, ".../div[3]/span//text()" which selects all descendant text. After spending quite some time searching (before the post, believe you me), I did end up finding help here Get text content of an HTML element using XPath?

I will leave this up to help others in the future.

Upvotes: 2

Related Questions