can't get <p>'s text when useing python scrapy a website
<p>Here is the website's source html code:</p>

<pre><code>      &lt;p class="fc-gray"&gt;
           hello
       &lt;span class=""&gt;2010-10&lt;/span&gt; 
       &lt;em class="shuxian"&gt;|&lt;/em&gt;
             4.2                 
      &lt;/p&gt;
</code></pre>

<p>I want to get the value 4.2.
The following is my code (carInfoDiv is a xpath element selector): </p>

<pre><code>        miles = carInfoDiv.xpath("p[contains(@class,'fc-gray')]/text()").extract()[0]
</code></pre>

<p>in this way, I got the string 'hello', I have also tried string(.), but got all string in <code>&lt;p&gt;</code>, not the result I wanted. Please show me how to get only 4.2 in this situation.</p>

<p>You are after the last text child of the <code>&lt;P&gt;</code> element, so you can add a <code>[last()]</code> predicate to your XPath expression:</p>

<pre><code>&gt;&gt;&gt; import scrapy
&gt;&gt;&gt; s = scrapy.Selector(text="""      &lt;p class="fc-gray"&gt;
...            hello
...        &lt;span class=""&gt;2010-10&lt;/span&gt; 
...        &lt;em class="shuxian"&gt;|&lt;/em&gt;
...              4.2                 
...       &lt;/p&gt;""")
&gt;&gt;&gt; s.xpath('.//p[@class="fc-gray"]/text()[last()]')
[&lt;Selector xpath='.//p[@class="fc-gray"]/text()[last()]' data='\n             4.2                 \n     '&gt;]
&gt;&gt;&gt; s.xpath('.//p[@class="fc-gray"]/text()[last()]').extract_first()
'\n             4.2                 \n      '
&gt;&gt;&gt; s.xpath('.//p[@class="fc-gray"]/text()[last()]').extract_first().strip()
'4.2'
&gt;&gt;&gt;&gt; # alternative using XPath's normalize-space() to do the whitespace stripping
&gt;&gt;&gt; s.xpath('normalize-space(.//p[@class="fc-gray"]/text()[last()])').extract_first()
'4.2'
</code></pre>

python scrapy