ExtractHTMLTables

Find tables inside HTML and extract the contents into objects using the following logic: - If table has a single column, just create an array of strings from the values - If table has 2 columns and has no header row, treat the first column as key and second as value and create a table of key/value - If table has a header row, create a table of objects where attribute names are the headers - If table does not have a header row, create table of objects where attribute names are cell1, cell2, cell3...

python · Common Scripts

Details

IDExtractHTMLTables
Languagepython
From Version5.0.0
Docker Imagedemisto/bs4-py3:1.0.0.10120494
TagsUtility

README

Find tables inside HTML and extract the contents into objects using the following logic:

  • If the table has a single column, just create an array of strings from the values.
  • If the table has 2 columns and has no header row, treat the first column as the key and the second column as the value and create a table for the key/value.
  • If the table has a header row, create a table of objects where the attribute names are the headers.
  • If the table does not have a header row, create table of objects where attribute names are cell1, cell2, cell3…

Script Data


Name Description
Script Type python
Tags Utility

Inputs


Argument Name Description
html The HTML to extract the tables from.
indexes Extracts only the tables with given indexes. IT will be, 0 based.

Outputs


Path Description Type
HTMLTables The extracted HTML tables. Unknown
from ExtractHTMLTables import extract_html_table


def test_extract_html_table():
    """
    Given:
        An html table.

    When:
        Execute command extract_html_table

    Then:
        Validate the right output returns.
    """
    html_table = """
        <table>
          <tr>
            <th>Company</th>
            <th>Contact</th>
            <th>Country</th>
          </tr>
          <tr>
            <td>Alfreds Futterkiste</td>
            <td>Maria Anders</td>
            <td>Germany</td>
          </tr>
          <tr>
            <td>Centro comercial Moctezuma</td>
            <td>Francisco Chang</td>
            <td>Mexico</td>
          </tr>
        </table>
        """
    res = extract_html_table(html_table, [])
    assert "Found 1 tables in HTML." in res["Contents"]


def test_no_html_table():
    """
    Given:
        An html text without a table.

    When:
        Execute command extract_html_table

    Then:
        Validate the right output returns.
    """
    html_table = """"""
    res = extract_html_table(html_table, [])
    assert "Did not find tables in HTML." in res