python scrapy remove the blanks from the answer

1

I am trying to make a scrapy script and I am not capable of what the strip method removes the blanks ... It should be a string but it is an object. Any way to do it?

import scrapy

class GamesSpider(scrapy.Spider):
    name = "games"
    start_urls = [
        'myurl',
    ]

    def parse(self, response):
        for game in response.css('ol#products-list li.item'):
            yield {
                'name': game.css('h2.product-name a::text').extract_first().strip(),
                'age': game.css('.list-price ul li:nth-child(1)::text').extract_first(),
                'players': game.css('.list-price ul li:nth-child(2)::text').extract_first(),
                'duration': game.css('.list-price ul li:nth-child(3)::text').extract_first(),
                'dimensions': game.css('.list-price ul li:nth-child(4)::text').extract_first()
            }
    
asked by user1551211 20.12.2018 в 09:15
source

1 answer

3

Of course you can use strip, or you can also do it at the level of Selector with xpath using the function normalize-space :

game.xpath('normalize-space(.//h2[contains(@class, "product-name")]//a/text())').extract_first(default='')

Also remember to use extract_first(default='') if you always want a string in response since extract_first can also return None when nothing was found.

    
answered by 20.12.2018 в 16:45