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>Strictly speaking it should be <code>response.xpath('(//*[@class="listing-title"])[1]/text()')</code> but if what you want is to grab the title of each ad (to create an item for example) you should probably do this instead:</p>

<pre><code>for article in response.xpath('//article[@data-q]'):
     item = GumtreeItem()
     item['title'] = article.css('.listing-title::text').extract_first()
     yield item
</code></pre>

python python-3.x xpath web-scraping scrapy