ItemAdapter

0.13.1 · active · verified Fri Apr 10

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.

Warnings

Install

Imports

Quickstart

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.

from dataclasses import dataclass
from itemadapter import ItemAdapter

# Define a simple dataclass item
@dataclass
class ProductItem:
    name: str
    price: float
    tags: list[str]

# Create instances of different item types
dataclass_item = ProductItem(name="Wireless Mouse", price=25.99, tags=["electronics", "peripherals"])
dict_item = {"name": "Mechanical Keyboard", "price": 120.00, "tags": ["gaming", "peripherals"]}

# Use ItemAdapter to interact with items uniformly
adapter_dc = ItemAdapter(dataclass_item)
adapter_dict = ItemAdapter(dict_item)

print(f"Dataclass Item (Type: {type(adapter_dc.item)}):")
print(f"  Field Names: {adapter_dc.field_names()}")
print(f"  Name: {adapter_dc['name']}")
print(f"  Price (get_value): {adapter_dc.get_value('price')}")

print(f"\nDictionary Item (Type: {type(adapter_dict.item)}):")
print(f"  Field Names: {adapter_dict.field_names()}")
print(f"  Name: {adapter_dict['name']}")
print(f"  Price (get_value): {adapter_dict.get_value('price')}")

# ItemAdapter also supports setting values if the underlying item is mutable
if adapter_dc.is_mutable:
    adapter_dc['price'] = 22.99
    print(f"\nUpdated dataclass item price: {adapter_dc['price']}")

view raw JSON →