Skip to main content

sveden-table-matrixizer

Extract and matrixize HTML tables from Russian educational organization pages (/sveden) with full colspan/rowspan support.

sveden-table-matrixizer is an asynchronous Python library that parses tables found on /sveden (сведения об образовательной организации) pages, expands colspan and rowspan attributes into clean two-dimensional matrices, and returns structured head and body cell grids. It gives you full control over edge cases via configurable callbacks.

Features

  • Async page fetching using aiohttp.
  • Full colspan/rowspan expansion – cells spanning multiple rows and/or columns are duplicated into the matrix.
  • Separate head and body matrix extraction – each table yields a head matrix (from <thead>) and a body matrix (from <tbody>).
  • Customizable error handling – replace default reactions to missing headers, missing bodies, or multiple <thead>/ <tbody> elements.
  • Lightweight – only depends on aiohttp and beautifulsoup4.
  • Works with any HTML – designed for /sveden, but usable on any page containing <table> elements.

Installation

pip install sveden-table-matrixizer

Quick Start

import asyncio
from sveden_table_matrixizer import matrixize_tables_from_page


async def main():
    url = "https://example.edu/sveden/"
    tables = await matrixize_tables_from_page(url)

    for i, table in enumerate(tables):
        print(f"Table {i + 1}:")
        print("  Head rows:", len(table.head))
        print("  Body rows:", len(table.body))
        # Access cells as list[list[bs4.Tag]]


asyncio.run(main())

Each MatrixizedTable contains:

  • head: list[list[Tag]] – rows of header cells (each row is a list of bs4.Tag).
  • body: list[list[Tag]] – rows of body cells.

Handling Edge Cases

By default, the extractor raises exceptions when a table lacks a header or body, or contains more than one <thead> / <tbody>. You can override this behavior with ExtractorOptions.

from sveden_table_matrixizer import matrixize_tables_from_page, ExtractorOptions
from sveden_table_matrixizer.def_funcs import MatrixizedTable


def handle_missing_header(table_tag, collected_tables):
    print(f"Skipping table without <thead>: {table_tag.get('id', 'no id')}")


opts = ExtractorOptions(
    on_table_no_header=handle_missing_header,
    # other callbacks can be set similarly
)

tables = await matrixize_tables_from_page(url, options=opts)

You can also supply async callbacks – the library automatically detects and awaits them.

API Reference

matrixize_tables_from_page(url, *, options=None)

  • Parameters:
    • url (str) – URL of the page to scrape.
    • options (ExtractorOptions, optional) – configuration callbacks.
  • Returns: list[MatrixizedTable] – extracted and matrixized tables.

ExtractorOptions

A frozen dataclass with the following fields (all optional):

Field Type Default Description
on_table_no_header Callable[[Tag, Sequence[MatrixizedTable]], Any] no‑op Called when a table has no <thead>
on_table_no_body Callable[[Tag, Sequence[MatrixizedTable]], Any] no‑op Called when a table has no <tbody>
on_multiply_table_headers Callable[[Sequence[Tag]], Tag | Never] raises MultiplyTableHeaderError Called when more than one <thead> is found; must return a single <thead> element.
on_multiply_table_bodies Callable[[Sequence[Tag]], Tag | Never] raises MultiplyTableBodyError Called when more than one <tbody> is found; must return a single <tbody> element.

MatrixizedTable

@dataclass(frozen=True, kw_only=True)
class MatrixizedTable:
    head: list[list[Tag]]  # matrix of <th> tags
    body: list[list[Tag]]  # matrix of <td> tags

Exceptions

  • NoTableHeaderError – raised when options.on_table_no_header is not overridden.
  • NoTableBodyError – raised when options.on_table_no_body is not overridden.
  • MultiplyTableHeaderError – default reaction to multiple <thead> elements.
  • MultiplyTableBodyError – default reaction to multiple <tbody> elements.

All exceptions are exported from sveden_table_matrixizer.errors.

How It Works

  1. The page is fetched with aiohttp and parsed by BeautifulSoup.
  2. All <table> tags are collected.
  3. For each table:
    • The <thead> is located; if missing or duplicate, the appropriate callback is invoked.
    • The <tbody> is located similarly.
    • Header rows are expanded: each <th> with colspan/rowspan is replicated into the correct cells of a 2D list.
    • The same expansion is applied to body rows using <td> elements.
  4. A MatrixizedTable(head=..., body=...) is created and added to the result list.

Dependencies

License

This project is licensed under the MIT License – see the source repository for details.

Metadata

Release files for sveden-table-matrixizer 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sveden-table-matrixizer 1.0.2
File Size Uploaded
sveden_table_matrixizer-1.0.2.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sveden-table-matrixizer 1.0.2
File Interpreter ABI Platform
sveden_table_matrixizer-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 18.0 kB

Release files / sveden_table_matrixizer-1.0.2.tar.gz

Download URL sveden_table_matrixizer-1.0.2.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
85caaf2fe020c37518cf78b2ffb452fdb3fbd68a7e130740ded4b45afdd7b5ee
BLAKE2b-256 checksum
How to use checksums
99439afbd46259d4e9c73290b941e63d86d881e687fe45a7ca75d8d323b41ac5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Release files / sveden_table_matrixizer-1.0.2-py3-none-any.whl

Download URL sveden_table_matrixizer-1.0.2-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
50fae3c36d320325ceacc042dbfbfb1d7e1340fa9aaceb46f659e9d903894627
BLAKE2b-256 checksum
How to use checksums
fa8b88e4006fba13185e27a41f2079b0fd586de299d290142b3674bfb9053be1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page