Skip to content Skip to sidebar Skip to footer

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,

Solution 1:

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

Post a Comment for "How To Check For Unfound Value With Add_xpath In Scrapy And Set Item Value To A Default Value?"