Scrapy – shell always returns ‘1’ when using xpath to get data/value of element

Trying to get value from webpage in scrapy shell. The html code is below

<tbody>
  <tr...>
   <th...>
     <td class="inventory" data-stat="coupes">4</td>
     <td class="inventory" data-stat="sedans">2</td>

What I currently see in the terminal

>>> response.xpath('//tbody/tr/td/@data-stat="coupes"').extract()
['1']
>>> response.xpath('//tbody/tr/td/@data-stat="sedans"').extract()
['1']
>>>

Can’t figure out why I’m getting the same response when values (4 & 2) are clearly different? Is the ‘1’ that’s being returned equivalent to ‘True’? I didn’t want to use index (data-stat[0] or [1]) because I thought using the names “coupes” or “sedans” would be more specific but I can’t get my head around this.

Answer

Your xpath is wrong. It should be like below

response.xpath('//tbody/tr/td[@data-stat="coupes"]/text()').extract()

Leave a Reply

Your email address will not be published. Required fields are marked *