ParseHTMLTables

Find tables inside HTML and extract the contents into objects using the following logic: - 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 · Filters And Transformers

Details

IDParseHTMLTables
Languagepython
From Version6.5.0
Docker Imagedemisto/bs4-py3:1.0.0.10120494
Tagstransformer general

README

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

  • 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…

Script Data


Name Description
Script Type python3
Tags transformer, general

Inputs


Argument Name Description
value The HTML to extract tables from
title The title for tables
filter_indexes Extract only the tables with given indexes - 0 based
filter_titles Extract only the tables with given titles

Outputs


There are no outputs for this script.

import json

import demistomock as demisto
import pytest

INPUTS = [
    ("test_data/test1-in.html", "test_data/test1-out.json", None, None),
    ("test_data/test2-in.html", "test_data/test2-out.json", None, None),
    ("test_data/test3-in.html", "test_data/test3-out.json", None, None),
    ("test_data/test4-in.html", "test_data/test4-out.json", None, None),
    ("test_data/test5-in.html", "test_data/test5-out.json", None, None),
    ("test_data/test6-in.html", "test_data/test6-out.json", None, None),
    ("test_data/test7-in.html", "test_data/test7-out.json", None, None),
    ("test_data/test8-in.html", "test_data/test8-out.json", None, None),
    ("test_data/test9-in.html", "test_data/test9-out.json", None, None),
    ("test_data/test10-in.html", "test_data/test10-out.json", None, None),
    ("test_data/test11-in.html", "test_data/test11-out.json", None, None),
    ("test_data/test12-in.html", "test_data/test12-out.json", None, "first_row"),
    ("test_data/test13-in.html", "test_data/test13-out.json", None, "first_column"),
    ("test_data/test14-in.html", "test_data/test14-out.json", None, None),
    ("test_data/test15-in.html", "test_data/test15-out.json", None, None),
    ("test_data/test16-in.html", "test_data/test16-out.json", None, None),
    ("test_data/test17-in.html", "test_data/test17-out.json", None, None),
    ("test_data/test18-in.html", "test_data/test18-out.json", None, None),
]


@pytest.mark.parametrize("in_file, out_file, title, default_header_line", INPUTS)
def test_main(mocker, in_file, out_file, title, default_header_line):
    from ParseHTMLTables import main

    with open(in_file) as f:
        value = f.read()

    with open(out_file) as f:
        expected = json.loads(f.read())

    mocker.patch.object(
        demisto, "args", return_value={"value": value, "title": title, "default_header_line": default_header_line}
    )
    mocker.patch.object(demisto, "results")
    main()
    assert demisto.results.call_count == 1
    results = demisto.results.call_args[0][0]
    assert json.dumps(results) == json.dumps(expected)