Using xpath to extract date
<p>I'm trying to extract the date from a webpage, '07/18/16' in the comment below. I'm not clear on the syntax for xpath, how would you grab just the date?</p>

<pre><code>#&lt;p&gt;Opened &lt;a class="timeline" href="/trac3/timeline?from=2016-07-    
#18T14%3A46%3A43-04%3A00&amp;amp;precision=second" title="See timeline at   
#07/18/16 14:46:43"&gt;6 weeks ago&lt;/a&gt;&lt;/p&gt;

from lxml import html
import requests

page = requests.get(webpage)
tree = html.fromstring(page.content)

openDate = tree.xpath('//Opened/text()')

print 'Open Date: ', openDate
</code></pre>

<p>Here's one way only using xpath 1.0:</p>

<pre><code>substring-before(substring-after(normalize-space(//a[contains(concat(' ',normalize-space(@class),' '),' timeline ')]/@title),'See timeline at '), ' ')
</code></pre>

<p>The <code>contains(concat(' ',normalize-space(@class),' '),' timeline ')</code> might seem like overkill, but will account for the possibility of classes other than "timeline" being in the class attribute.</p>

<p>XPath test: <a href="http://www.xpathtester.com/xpath/7805b0601b1468ea17209127e14fa470" rel="nofollow">http://www.xpathtester.com/xpath/7805b0601b1468ea17209127e14fa470</a></p>

<p><strong>lxml Example</strong></p>

<pre class="lang-py prettyprint-override"><code>from lxml import html

page = """&lt;p&gt;Opened &lt;a class="timeline" href="/trac3/timeline?from=2016-07-18T14%3A46%3A43-04%3A00&amp;amp;precision=second" title="See timeline at 07/18/16 14:46:43"&gt;6 weeks ago&lt;/a&gt;&lt;/p&gt;"""
tree = html.fromstring(page)

try:
    openDate = tree.xpath("substring-before(substring-after(normalize-space(//a[contains(concat(' ',normalize-space(@class),' '),' timeline ')]/@title),'See timeline at '), ' ')")
    print 'Open Date: ', openDate
    #Open Date: 07/18/16
except:
    print("Something went wrong")
</code></pre>

python regex xpath python-requests lxml