Fields
pagemodel.fields
Fields package for pagemodel.
This package provides the building blocks for declarative HTML/XML extraction.
Core descriptors
BaseField– abstract base class for all field descriptors, implementing caching, pipeline support, and export registration.Field– unified field descriptor that replaces all specialised types from earlier versions (TextField, AttrField, etc.). Supports CSS and XPath selectors, text/attribute extraction, JSON parsing with optional Pydantic validation, fragment wrapping, and multiple-value modes.XpathField– specialised descriptor for raw XPath queries, returning the exact result ofdoc.xpath()without automatic text extraction.
Fragment support
fragment– class decorator that turns a plain class into anHtmlFragmentand wraps it in aFragmentDescriptorfor use on a page.FragmentDescriptor– descriptor that extracts elements matching a CSS selector, instantiates the associated fragment class, and caches the result.
Utilities
export– decorator that marks a method for inclusion inexport()output.
All public symbols are re‑exported from the package root for convenience.
BaseField
Base descriptor for all field types.
Provides caching, pipeline support, automatic registration for export,
and low‑level CSS/XPath query execution. Concrete field classes must
implement _get(instance, owner).
Methods:
__get__
__get__(instance, owner)
Extract the value on first access, apply the pipeline, and cache.
__or__
__or__(other)
Add a pipeline step (or pipe) to the field.
Field
Universal field descriptor.
Automatically detects CSS vs XPath selectors, pre‑compiles them, and generates a branch‑free extractor for maximum speed.
| Parameters: |
|
|---|
FragmentDescriptor
Descriptor for @fragment decorated classes. Caches the extracted
fragment(s) on the page instance.
| Parameters: |
|
|---|
fragment_cls
property
fragment_cls
The fragment class used to wrap matched elements.
Methods:
__get__
__get__(instance, owner)
Extract fragment(s) on first access and cache the result in the instance dictionary.
XpathField
Extract data using an XPath expression.
Returns the raw result of doc.xpath(selector), typically a list of
strings or elements. Use index to pick a specific value.
| Parameters: |
|
|---|
Functions:
export
export(arg)
Decorator that marks a method for inclusion in export().
Can be used without arguments::
@export
def computed_value(self):
return ...
or with a custom export name::
@export("custom_name")
def some_method(self):
return ...
fragment
fragment(
selector, *, multiple=False, default=None, name=None
)
Class decorator that turns a plain class into an HtmlFragment and
returns a FragmentDescriptor for use on a BasePage.
Usage::
class Page(BasePage):
@fragment(".product", multiple=True)
class Product:
name: str = ".name"
price: float = ".price @data-value"
| Parameters: |
|
|---|