TextFromHTML

Extract regular text from the given HTML.

python · Common Scripts

Source

import re

import demistomock as demisto  # noqa: F401
from CommonServerPython import *  # noqa: F401


def get_plain_text(html: str, replace_line_breaks: bool, trim_result: bool):
    data = ""
    if html:
        data = re.sub(r"<\/?br\s?\/?>", "\n", html, flags=re.IGNORECASE) if replace_line_breaks else html

        data = re.sub(r"<.*?>", "", data)
        entities = {
            "quot": '"',
            "amp": "&",
            "apos": "'",
            "lt": "<",
            "gt": ">",
            "nbsp": " ",
            "copy": "(C)",
            "reg": "(R)",
            "tilde": "~",
            "ldquo": '"',
            "rdquo": '"',
            "hellip": "...",
        }
        for e in entities:
            data = data.replace(f"&{e};", entities[e])

        if trim_result:
            data = re.sub(r"[ \t]{2,}", " ", data)
            data = re.sub(r"(\s*\r?\n){3,}", "\n\n", data)
            data = data.strip()
    return data


def get_body(html: str, html_tag: str, allow_fallback: bool = False):
    if html and html_tag:
        body = re.search(rf"<{html_tag}.*/{html_tag}>", html, re.MULTILINE + re.DOTALL + re.IGNORECASE + re.UNICODE)

        if body and body.group(0):
            return body.group(0)
        elif allow_fallback and html_tag.lower() == "body":
            return html
    return ""


def main():  # pragma: no cover
    try:
        args = demisto.args()
        html = args["html"]
        html_tag = args.get("html_tag", "body")
        allow_fallback = argToBoolean(args.get("allow_body_fallback", "false"))
        replace_line_breaks = argToBoolean(args.get("replace_line_breaks", "false"))
        trim_result = argToBoolean(args.get("trim_result", "false"))
        context_path = argToBoolean(demisto.args().get("output_to_context", "false"))

        body = get_body(html, html_tag, allow_fallback)
        text = get_plain_text(body, replace_line_breaks, trim_result)
        text = text if text != "" else "Could not extract text"

        result = CommandResults(
            outputs_prefix="TextFromHTML",
            outputs=text if context_path else None,
            raw_response=text,
            readable_output=text,
        )

        return_results(result)
    except Exception as ex:
        return_error(message="Failed to extract text", error=ex)


if __name__ in ["__builtin__", "builtins"]:
    main()

README

Extract regular text from the given HTML

Script Data


Name Description
Script Type python3
Tags Utility
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
html The HTML to strip tags from.
html_tag Specify the HTML tag to extract the text from within.
allow_body_fallback Allow using the input HTML as a fallback for the body, if no body tag is found. This only applies, if html_tag is set to body.
replace_line_breaks Replace `br` in `html` with linebreaks in the output.
trim_result Trim the extracted result. When set to true, leading and trailing whitespaces are removed and blocks of more than 3 consecutive whitespaces are collapsed to two.
output_to_context Store the extracted text in context.

Outputs


Path Description Type
TextFromHTML The Text extracted from the given HTML. string

Script Examples

Example command

!TextFromHTML html="<!DOCTYPE html><html><body><h1>This is heading 1</h1></body></html>"

Context Example

{}

Human Readable Output

This is heading 1