Page & Fragments

pagemodel.page

Page and fragment base classes.

  • BasePage represents an entire HTML/XML document.
  • BaseFragment / HtmlFragment represent a subtree within a document.
  • StreamPage provides iterative XML processing without loading the whole tree.

BaseFragment

Represents a subtree of a document.

Parameters:
  • elm (HtmlElement) –

    The root element of the fragment.

  • _copy (bool, default: False ) –

    If True, a deep copy of elm is made before use.

source cached property
source

The serialised XML of this fragment's subtree.

BasePage

Main entry point for extracting data from an HTML/XML document.

Accepts a string, bytes, or pre‑parsed lxml.html.HtmlElement. Fields declared on the class are automatically resolved via _process_annotations.

Parameters:
  • source (str, bytes, or HtmlElement) –

    The HTML/XML content.

  • url (str, default: None ) –

    Base URL used to resolve relative links.

  • encoding (str, default: 'utf-8' ) –

    Encoding used when source is bytes. Default "utf-8".

doc cached property
doc

The parsed lxml document tree.

source cached property
source

The original HTML/XML source as a string.

HtmlFragment

Alias for BaseFragment – kept for backward compatibility.

StreamPage

Iterative XML processor that yields exported dictionaries.

Useful for large XML files where building a full DOM is expensive.

Parameters:
  • source (str or bytes) –

    The XML content.

  • encoding (str, default: 'utf-8' ) –

    Encoding used when source is bytes. Default "utf-8".

  • tag_callback_map (dict, default: None ) –

    Mapping of XML tag names to fragment classes. Each time a matching element is parsed, the fragment is instantiated and exported.

Methods:
iter_items
iter_items(tag)

Yield exported dictionaries for each occurrence of tag.