Itemloaders is a base library for Scrapy's ItemLoader, providing a robust and flexible way to parse and populate Scrapy Items. It handles data extraction from various sources (XPath, CSS, regular expressions, JMESPath) and processes it through a chain of input and output processors. The current version is 1.4.0, and the library maintains an active release cadence, frequently updating Python version support.
pip install itemloadersVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a simple Item, create an `ItemLoader` inheriting from `itemloaders.ItemLoader`, and use CSS selectors, XPath, and custom processors (`MapCompose`, `TakeFirst`) to extract and process data from an HTML string using `parsel.Selector` to populate the item fields.
Ensure your project's Python version meets the minimum requirements for the `itemloaders` version you are using. Check the release notes for specific version requirements before upgrading.
Upgrade to version 1.3.1 or newer, which includes a fix for this issue.
Upgrade to version 1.0.6 or newer, which fixed this regression. If constrained to 1.0.5, ensure the `re` parameter is always a string pattern, or avoid using compiled patterns.
If using JMESPath features, ensure your `parsel` dependency is explicitly set to `parsel>=1.8.1`.
pip install itemloaders
Ensure the ItemLoader is instantiated with a valid Scrapy Item or by setting `default_item_class` if creating an item automatically. Also, verify that the `response` or `selector` passed to the ItemLoader is not None.
Example:
from scrapy.loader import ItemLoader
from myproject.items import ProductItem
def parse(self, response):
if response is None:
self.logger.warning("Received a None response, skipping ItemLoader initialization.")
return
loader = ItemLoader(item=ProductItem(), response=response)
# ... add_xpath, add_css, etc.
yield loader.load_item()Instantiate the `ItemLoader` with a `response` object or a `Selector` object.
Example:
from scrapy.loader import ItemLoader
from myproject.items import MyItem
def parse(self, response):
loader = ItemLoader(item=MyItem(), response=response)
# Now you can use add_xpath or add_css
loader.add_xpath('name', '//h1/text()')
yield loader.load_item()Ensure `TakeFirst()` is correctly assigned as an `_out` processor for a specific field or as the `default_output_processor` for the ItemLoader. Also, verify that the data reaching the output processor is indeed an iterable.
Example:
from itemloaders.processors import TakeFirst, MapCompose
from scrapy.loader import ItemLoader
class ProductLoader(ItemLoader):
default_output_processor = TakeFirst() # Applies to all fields by default
# Or for a specific field:
name_out = TakeFirst()
description_in = MapCompose(str.strip)
description_out = Join()