lxml XPath position() does not work
<p>I tried to scrape a page via XPath but I could not get it work as expected.</p>

<p>The page is like,</p>

<pre><code>&lt;tag1&gt;
    &lt;tag2&gt;
          ....
              &lt;div id=article&gt;
                  &lt;p&gt; stuff1 &lt;/p&gt;
                  &lt;p&gt; stuff2 &lt;/p&gt;
                  &lt;p&gt; ...... &lt;/p&gt;
                  &lt;p&gt; stuff30 &lt;/p&gt;
</code></pre>

<p>I want to extract <code>stuff1</code> through <code>stuff30</code> as string. Here is my Python code snippet.</p>

<pre><code>import lxml.html
import urllib.request

html = urllib.request.urlopen('http://www.something.com/news/blah/').read()
root = lxml.html.fromstring(html)

content = root.xpath('string(//div[@id="article"]/p[position()=&gt;1 and position()&lt;=last()]/.)')
</code></pre>

<p>This code did not return anything.</p>

<p>If I rewrite from <code>position()</code> statement to individual element index, it works.</p>

<pre><code>content = root.xpath('string(//div[@id="article"]/p[25]/.)')
</code></pre>

<p>This code returns <code>stuff25</code> correctly.</p>

<p>I don't want to run for loop just for this. I believe there is a way to get my code work with <code>position()</code>, but not sure what's wrong in my code.</p>

<p>Thats because you have position()=>1, should be position()>=1</p>

<pre><code>content = root.xpath('string(//div[@id="article"]/p[position()&gt;=1 and position()&lt;=last()]/.)')
</code></pre>

<p>will set content to stuff1.</p>

python python-3.x xpath