Model structured scraped data
A spider extracts consistent attributes such as a product name and price.
Scrapy · Class
scrapy.Item is a dictionary-like container for structured data extracted by spiders. Define its permitted fields with scrapy.Field() and populate instances using normal mapping operations.
Subclass scrapy.Item to define the fields a spider may produce. Each field is declared with scrapy.Field(), while the resulting object behaves like a dictionary for storing and accessing values; its fields mapping retains declarations and metadata.
class scrapy.Item(**args: Any, **kwargs: Any)| Name | Type | Description |
|---|---|---|
args | Any | Optional positional mapping or iterable accepted by the dictionary-like initializer. |
kwargs | Any | Field values supplied by name during initialization. |
A new Item instance initialized from the supplied arguments.
scrapy.Item when modeling structured data emitted by a spider.KeyError.import scrapy
class Product(scrapy.Item):
name = scrapy.Field()
price = scrapy.Field()
product = Product(name="Book", price="12.99")
print(product["name"])
print(product.fields.keys())Declares fields on a custom item, initializes values with keyword arguments, reads a value using dictionary syntax, and inspects all declared fields.
A spider extracts consistent attributes such as a product name and price.
A project needs exporters or pipelines to know the available output fields.
A team wants misspelled field names to fail instead of silently adding keys.
A field needs processing-related metadata for later Scrapy components.
scrapy.Item, and how do you define one?Item is Scrapy's feature-complete item type with a dictionary-like API. A custom item subclasses scrapy.Item and declares its allowed fields with Field attributes.
Follow-up: How does Scrapy recognize an item for pipelines? The documentation says Item is supported by itemadapter, so spider callback results of this type can be passed to item pipelines.
The class declaration creates the permitted field names, and instances store values using the standard dictionary interface. The declaration metadata is available through the instance's fields attribute.
Follow-up: What does fields contain? It contains the declared field definitions, including fields that have not yet received values.
Item instead of a plain dictionary?An Item replicates the standard dictionary API, including its initializer, but limits keys to declared fields. Therefore, assigning or referring to an undefined field raises KeyError, helping expose spelling mistakes.
Follow-up: What alternative item types does Scrapy support? The documentation identifies dictionaries, dataclasses, and attrs objects through itemadapter as well as Item objects.
scrapy.FieldItem.fieldsitemadapteritem pipelinesItem.copy()Item.deepcopy()