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>XPath works by matching elements in an XML structured document.</p>

<p>Your XPath will fail, because what you are saying is search the entire document ("//") for any elements called "Opened" (i.e. <code>&lt;Opened/&gt;</code>) and return their inner text ("text()").</p>

<p>Assuming your HTML is consistent, what you actually want to do is scrape the contents of the anchor titles for the date, something like this: </p>

<pre><code>//p[contains(text(),'Opened')]/a[@class='timeline']/@title
</code></pre>

<p>this will search the entire document for any anchors which are of the class 'timeline' and that are within paragraphs containing the word 'Opened' and return the contents of their 'title' attributes.</p>

<p>Notice I said "any anchors"; you're result will be a list of matched titles, so you'll need to decide what to do if you have multiple matches.</p>

<p>Once you have the title(s), you'll need to do some string slicing in python to retrieve the date part.</p>

<p>I'm assuming it's just XPath you're struggling with so I have left out any python examples. I recommend this site as a great starting point for XPath: <a href="http://dh.obdurodon.org/introduction-xpath.xhtml" rel="nofollow">http://dh.obdurodon.org/introduction-xpath.xhtml</a></p>

python regex xpath python-requests lxml