XO39
XO39

Reputation: 483

How to check for unfound value with add_xpath in scrapy and set item value to a default value?

Let's say I have this code:

def parse(self, response)
    l = ItemLoader(item=MyItem(), response=response
    l.add_xpath('title', '//*[@id="title"]/text()', MapCompose(str.strip, str.title)
    l.add_xpath('price', '//*[@id="price"]/text()', MapCompose(lambda i: i.replace(',', ''), float), re = '[,.0-9]')

    return l.load_item()

I want to check if no element found, in that case set a default value with l.add_value, for example (I tried doing this):

    if l.add_xpath('price', '//*[@id="price"]/text()', MapCompose(lambda i: i.replace(',', ''), float), re = '[,.0-9]'):
        l.add_value('available', 1)
    else:
        l.add_value('price', 0)
        l.add_value('available', 0)

But I got odd results (see discussion here).
Any ideas to achieve that?

Thanks in advance.

Upvotes: 0

Views: 558

Answers (1)

Gerrat
Gerrat

Reputation: 29700

The source for ItemLoader is here. The add_xpath method doesn't return anything (and so by default it returns None, so your if statement will always be False).

There does appear to be a get_xpath method that would appear to do what you want (check if the element exists).

Upvotes: 2

Related Questions