from scrapy.loader import ItemLoader from myproject.items import BookItem class BookLoader(ItemLoader): default_item_class = BookItem default_output_processor = TakeFirst() title_in = MapCompose(str.strip) author_in = MapCompose(str.strip) price_in = MapCompose(str.strip)
from scrapy.loader import ItemLoader from myproject.items import ProductItem class ProductLoader(ItemLoader): default_item_class = ProductItem default_output_processor = Compose(lambda v: v[0] if v else None) name_in = MapCompose(str.strip) price_in = MapCompose(lambda x: x.replace('$', '').strip())In this example, we define a ProductLoader class that also inherits from ItemLoader. We set the default_item_class to be ProductItem, which is the item that will be loaded. We define some input processors (name_in, price_in) using MapCompose(), which will process the data before loading it into the item. Finally, we set the default_output_processor to Compose(), which will apply a lambda function that takes the first value from a list if it exists, or returns None if the list is empty. In both examples, we use the load_item() method to load the items with the loaders we defined. We can call the load_item() method in a Scrapy spider to extract data from a web page and populate the corresponding item.