Skip to main content

HTML table parser that supports rowspan, colspan, links and nested tables. Fast, lightweight with no external dependencies.

Project description

HTML Table Takeout

Test

HTML Table Takeout project logo

A fast, lightweight HTML table parser that supports rowspan, colspan, links and nested tables. No external dependencies are needed.

The input may be text, a URL or local file Path.

HTML5 logo by W3C.

Quick Start

Install the package:

pip install html-table-takeout

Pass in a URL and print out the parsed Table as CSV:

from html_table_takeout import parse_html

# start with http:// or https:// to source from a URL
tables = parse_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')

print(tables[0].to_csv())

# output:
# Symbol,Security,GICS Sector,GICS Sub-Industry,Headquarters Location,Date added,CIK,Founded
# MMM,3M,Industrials,Industrial Conglomerates,"Saint Paul, Minnesota",1957-03-04,0000066740,1902
# ...

Pass in HTML text and print out the parsed Table as valid HTML:

from html_table_takeout import parse_html

tables = parse_html("""
<table>
    <tr>
        <td rowspan='2'>1</td> <!-- rowspan will be expanded -->
        <td>2</td>
    </tr>
    <tr>
        <td>3</td>
    </tr>
</table>""")

print(tables[0].to_html(indent=4))

# output:
# <table data-table-id='0'>
# <tbody>
#     <tr>
#         <td>1</td>
#         <td>2</td>
#     </tr>
#     <tr>
#         <td>1</td>
#         <td>3</td>
#     </tr>
# </tbody>
# </table>

Usage

The core parse_html() function returns a list of zero or more top-level Table. A Table is guaranteed to have this structure:

  • rows: List of one or more TRow
    • cells: List of zero or more TCell resulting from rowspan and colspan expansion
      • elements: List of zero or more TText, TLink, TRef
Type Description
Table Each parsed table has an auto-assigned unique id
TRow Equal to each <tr> in the original table
TCell Expanded <td> or <th> cells from row/colspan
TText HTML-decoded text inside <td> or <th>
TLink Equal to each <a> inside <td> or <th>
TRef Reference to the child Table

All tables are guaranteed to have at least one TRow containing one TCell.

The parse_html() function also provides filtering by text or attributes to target the tables you want. Check out its docstring for all options.

Why did you make this

Most HTML table parsers require extra DOM and data processing libraries that aren't needed for my application. I need a parser that handles nesting and gives me the flexibility to process the parsed result however I want.

Now you too can take out tables to go.

Developing

Install development dependencies:

pip install build mypy pytest

Run tests:

pytest

Build the package:

python -m build

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

html_table_takeout-1.1.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

html_table_takeout-1.1.1-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file html_table_takeout-1.1.1.tar.gz.

File metadata

  • Download URL: html_table_takeout-1.1.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for html_table_takeout-1.1.1.tar.gz
Algorithm Hash digest
SHA256 302277471b859bad7a8d46053d95b75963b1b60dfe3a2d4b398e131cee91aa97
MD5 f30b28d5192523dccdcf4d4fe0fa379c
BLAKE2b-256 ce1212c69d265a9a17aa423fb4630140bcee15aeffc006190ccc67495be81a4b

See more details on using hashes here.

File details

Details for the file html_table_takeout-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for html_table_takeout-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 abca351af67daa74eefcbb0556d7b8e11581cfb60b140da64d5df43e67770b00
MD5 0845f6a5bed97edad976e1905bbf87e0
BLAKE2b-256 7db4f9f8efe772a26b3a8392f707d696b7dd0c473c1c15e5028d125cecf4226a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page