xpath cant select only one html tag
<p>I am trying to get some data from a website, but when i use the following code it's return all of the matched elements, i want to return only 1st match! I've tried extract_first but it returned none!</p>

<pre><code># -*- coding: utf-8 -*-
import scrapy
from gumtree.items import GumtreeItem



class FlatSpider(scrapy.Spider):
    name = "flat"
    allowed_domains = ["gumtree.com"]
    start_urls = (
        'https://www.gumtree.com/flats-for-sale',
    )

    def parse(self, response):
        item = GumtreeItem()
        item['title'] = response.xpath('//*[@class="listing-title"][1]/text()').extract()
        return item
</code></pre>

<p>How to select only one element with xpath selector ? </p>

<p>This is because the first element is actually empty - filter out the non-empty values only and use <code>extract_first()</code> - works for me:</p>

<pre><code>$ scrapy shell "https://www.gumtree.com/flats-for-sale" -s USER_AGENT="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36"
In [1]: response.xpath('//*[@class="listing-title"][1]/text()[normalize-space(.)]').extract_first().strip()
Out[1]: u'REDUCED to sell! Stunning Hove sea view flat.'
</code></pre>

python python-3.x xpath web-scraping scrapy