user10508851
user10508851

Reputation:

Scrapy get result in shell but not in script

one topic again ^^ Based on recommendations here, I've implemented my bot the following and tested it all in shell :

    name_list = response.css("h2.label.title::text").extract()
    packaging_list = response.css("div.label.packaging::text").extract()
    ean = response.css("h1.page-title::text").extract_first()
    product_price = ''.join(response.css('.product-pricing__main-price  ::text').extract())
    company = "carrefour"

    for name, packaging, price in zip(name_list, packaging_list, product_price):
        item = ScrapybotItem()
        item['ean'] = ean
        item['desc'] = name.replace("\n","").strip() + " " +  packaging
        item['price'] = price
        item['company'] = company

        yield item

Problem is with price field.

For price in shell, I have for instance :

In [2]: product_price
Out[2]: '\n                    5,65€\n\n  \n      '

Output from script for same product :

{'company': 'carrefour',
'desc': "Gel nettoyant anti-imperfections 5 en 1 L'Oréal Paris Men Expert 
 le "
     'tube de 150ml',
 'ean': '\n  1 résultat pour « 3600522418634 »\n',
 'price': '\n'}

Do you know why don't I get result for prices with script ?

Upvotes: 0

Views: 67

Answers (1)

Valdir Stumm Junior
Valdir Stumm Junior

Reputation: 4667

product_price is a string, given that you are joining the results of the selector in:

product_price = ''.join(response.css('.product-pricing__main-price  ::text').extract())

Then, when you use zip, you'll be splitting that string in parts, thus you'll have the \n for the first item, as it's probably the first character in product_price.

Check this example:

>>> for i, j, k in zip([1, 2, 3, 4], [5, 6, 7, 8], 'abcd'):
        print (i, j, k)

Output:

1 5 a
2 6 b
3 7 c
4 8 d

Upvotes: 1

Related Questions