lundi 22 janvier 2018

IF Statement within Scrapy item declaration

I'm using scrapy to build a spider to monitor prices on a website. The website isn't consistent in how it displays it's prices. For it's standard price, it always uses the same CSS class, however when a product goes on promotion, it uses one of two CSS classes. The CSS selectors for both are below:

response.css('span.price-num:last-child::text').extract_first()

response.css('.product-highlight-label')

Below is how my items currently look within my spider:

    item = ScraperItem()

    item['model'] = extract_with_css('.product-id::text')
    item['link'] = extract_with_css('head meta[property="og:url"]::attr(content)')
    item['price'] = extract_with_css('span.list-price:last-child::text')
    item['promo_price'] = extract_with_css('span.price-num:last-child::text')


    yield item`

I would like to have something like:

IF response.css('span.price-num:last-child::text') is true

item['promo_price'] = extract_with_css('span.price-num:last-child::text')

ELSE item['promo_price'] = extract_with_css('.product-highlight-label')

Each way I've tried this I have failed.

Aucun commentaire:

Enregistrer un commentaire