I'm starting to work with item loaders in scrapy,and the basic functionality is working fine as in:
l.add_xpath('course_title', '//*[@class="course-header-ng__main-info__name__title"]//text()')
But if I want to apply a funtion to this item, where do I define the function?
On this question there is an example:
from scrapy.loader.processors import Compose, MapCompose, Join, TakeFirst
clean_text = Compose(MapCompose(lambda v: v.strip()), Join())
to_int = Compose(TakeFirst(), int)
class MyItemLoader(ItemLoader):
default_item_class = MyItem
full_name_out = clean_text
bio_out = clean_text
age_out = to_int
weight_out = to_int
height_out = to_int
Does this goes instead of the custom template?:
import scrapy
class MoocsItem(scrapy.Item):
# define the fields for your item here like:
description = scrapy.Field()
course_title = scrapy.Field()
Can I use funtions that are one liners as?
clean_text = Compose(MapCompose(lambda v: v.strip()), Join())