ItemAdapter provides a common interface for various data container classes like dictionaries, dataclasses, attrs, Pydantic models, and Scrapy Items. It allows uniform access and manipulation of fields across different item types, simplifying data processing logic. The library is actively maintained by the Scrapy project, with frequent minor releases (roughly every 1-3 months) to keep up with Python versions and dependency updates. The current version is 0.13.1.
pip install itemadapterVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `ItemAdapter` to create a unified interface for different item types, specifically a Python dataclass and a dictionary. It shows how to access field names and values using both dictionary-like access and `get_value()`, and how to check if an item is mutable for value assignment.
Upgrade your Python environment to 3.9 or higher if using itemadapter versions 0.10.0+.
Remove any usage of these `itemadapter.utils` functions from your code. ItemAdapter's core functionality handles item type detection internally.
If you are using Pydantic v2, ensure you upgrade itemadapter to v0.11.0 or newer for full compatibility.
Install the library using pip: `pip install itemadapter`
Pass the actual data item to `ItemAdapter`, not an already-wrapped adapter. For example, if `adapter` is an `ItemAdapter` instance, use `adapter.item` to get the original item.
Before accessing attributes or methods, check if the retrieved value is a list. If it is, iterate through the list or access a specific element (e.g., `item_adapter['date_field'][0].date()` if you expect a single date within a list).
Ensure the field name is correct and present in the item. Alternatively, use the `.get()` method (e.g., `adapter.get('field_name', default_value)`) to provide a default value if the field might be missing, preventing a `KeyError`.No dependency data recorded yet.