IF Statement within Scrapy item declaration

徘徊边缘 提交于 2019-12-24 20:24:23

问题


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.


回答1:


I got it to work. Here's my code:

    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')

    if response.css('span.price-num:last-child::text'):
        item['promo_price'] = extract_with_css('span.price-num:last-child::text')
    else:
        item['promo_price'] = extract_with_css('.product-highlight-label::text')

    yield item


来源:https://stackoverflow.com/questions/48390005/if-statement-within-scrapy-item-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!