Extraction
A API de extração estruturada: defina um modelo e receba dados tipados e validados de volta. Saiba mais em Extração estruturada.
pydoll.extractor.model.ExtractionModel
Bases: BaseModel
Base class for declarative extraction models.
Inherits from pydantic.BaseModel, gaining automatic validation, type coercion, serialization (model_dump, model_dump_json), and JSON Schema generation (model_json_schema).
Subclasses define fields using Field() descriptors with selectors and/or semantic descriptions. The extraction engine uses this metadata to extract structured data from web pages.
Example::
class Article(ExtractionModel):
title: str = Field(selector='h1', description='Article title')
author: str = Field(selector='.author', description='Author name')
model_config
class-attribute
instance-attribute
get_extraction_fields
classmethod
Get extraction metadata for all fields, collecting lazily on first access.
Each subclass gets its own cache, even if a parent class has already been collected. This ensures inherited fields are included correctly.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, ExtractionMetadata]
|
Dictionary mapping field name to ExtractionMetadata. |
| RAISES | DESCRIPTION |
|---|---|
InvalidExtractionModel
|
If a field has metadata but lacks both selector and description. |
pydoll.extractor.field.Field
Field(*, selector=None, attribute=None, description=None, default=PydanticUndefined, transform=None)
Define extraction metadata for a model field.
Wraps pydantic.Field() and registers ExtractionMetadata for the engine. Auto-detects CSS vs XPath from selector syntax (same logic as Tab.query()).
At least one of selector or description must be provided:
- selector only: extracted via CSS/XPath.
- description only: metadata for future LLM extraction.
- both: CSS extraction with LLM fallback in future auto strategy.
| PARAMETER | DESCRIPTION |
|---|---|
selector
|
CSS or XPath selector (auto-detected, like Tab.query()).
TYPE:
|
attribute
|
HTML attribute to extract (default: innerText).
TYPE:
|
description
|
Semantic description of the field.
TYPE:
|
default
|
Default value if extraction fails. PydanticUndefined means required.
TYPE:
|
transform
|
Post-processing callable applied to raw extracted string.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FieldInfo
|
Pydantic FieldInfo with extraction registry key in json_schema_extra. |
| RAISES | DESCRIPTION |
|---|---|
InvalidExtractionModel
|
If neither selector nor description is provided. |