Skip to content

Html utils

get_target_input_data_by_regex_pattern(html, target_pattern)

Extracts target input data from HTML using specified regex patterns.

Parameters:

Name Type Description Default
html str

The HTML content to search for patterns.

required
target_pattern Dict[str, any]

A dictionary containing variable names as keys and regex patterns as values.

required

Returns:

Type Description
Dict[str, str]

Dict[str, str]: A dictionary mapping variable names to extracted data.

Source code in apps/process-input/source-watcher/source_watcher/jobs/handlers/default/html_utils.py
def get_target_input_data_by_regex_pattern(html: str, target_pattern: Dict[str, any]) -> Dict[str, str]:
    """
    Extracts target input data from HTML using specified regex patterns.

    Args:
        html (str): The HTML content to search for patterns.
        target_pattern (Dict[str, any]): A dictionary containing variable names as keys
            and regex patterns as values.

    Returns:
        Dict[str, str]: A dictionary mapping variable names to extracted data.

    Raises:
        None
            If the data for any variable is not found in the HTML.
    """
    result_search = dict()
    for var_name, _target_pattern in target_pattern.items():
        fetch_pattern = re.findall(_target_pattern, html)
        if fetch_pattern == []:
            return None
        result_search[var_name] = fetch_pattern[0].decode()
    return result_search