Declare item fields
Define the schema of scraped records with named attributes.
Scrapy · Class
scrapy.Field is a dictionary-like metadata container used to declare fields on Scrapy Item subclasses. It can store configuration such as a serializer, while Scrapy records declared fields in Item.fields.
Field is a metadata container used when declaring Scrapy item attributes. It behaves like a plain dictionary, so metadata can be supplied as keyword arguments; the declared Field objects are collected in the item’s fields mapping rather than remaining as class attributes.
class scrapy.Field| Name | Type | Description |
|---|---|---|
**kwargs | Any | Optional field metadata supplied as dictionary entries, commonly through keyword arguments such as serializer. |
A plain dictionary containing the supplied field metadata.
import scrapy
class Product(scrapy.Item):
name = scrapy.Field()
price = scrapy.Field(serializer=float)
product = Product(name="Book", price="12.50")
assert product["price"] == "12.50"
assert product.fields["name"] == {}Declare item fields with Field and optionally place metadata such as a serializer in the field mapping.
Define the schema of scraped records with named attributes.
Attach serializer or other processing metadata to a field.
Inspect the complete declared schema of an Item subclass.
Field is used to declare an item field and hold metadata for it. The class itself is a plain dictionary alias, without extra functionality or attributes.
Follow-up: Where can declaration metadata be inspected after class creation? In the Item.fields mapping, whose values are the Field objects used in the declaration.
A Field can be created without metadata or with metadata entries such as serializer=str. Those entries provide a central place to describe how the field is handled internally.
Follow-up: Does Field itself run the serializer? No; it stores metadata, while other Scrapy processing uses that metadata.
Fields are declared as class attributes on an Item subclass, for example name = scrapy.Field(). Scrapy processes those declarations and stores them in the item’s fields attribute.
Follow-up: What does the fields mapping contain? All declared fields, including fields that have not yet been populated.
scrapy.ItemItem.fieldsitemadapterserializer