You need to enable JavaScript to run this app.
AI 数据湖服务

AI 数据湖服务

复制全文
文本清洗
html 标签移除
复制全文
html 标签移除

算子介绍

描述

HTML文档净化处理器 - 多结构解析与智能清理解决方案

核心功能

  • 多结构解析
  • 标题提取:自动识别<h1>-<h6>标签
  • 正文抽取:智能识别文章主体内容
  • 冗余过滤:移除<script>/<style>等非文本标签
  • 智能处理
  • 容错机制:支持残缺HTML片段解析
  • 格式保留:维持文本段落结构与换行逻辑

技术实现

  • 基础库:BeautifulSoup4(html.parser)

Daft 调用

算子参数

输入

输入列名

说明

texts

包含HTML内容的文本数组,元素类型为字符串

输出

清理后的文本数组,元素类型为字符串

参数

如参数没有默认值,则为必填参数

参数名称

类型

默认值

描述

separator

str


strip

bool

True

调用示例

下面的代码展示了如何使用 daft 运行算子对 html 标签进行移除。

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.text.clean_html_tag import CleanHtmlTag
from daft.las.functions.udf import las_udf

if __name__ == "__main__":

    if os.getenv("DAFT_RUNNER", "native") == "ray":
        import logging

        import ray

        def configure_logging():
            logging.basicConfig(
                level=logging.INFO,
                format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
                datefmt="%Y-%m-%d %H:%M:%S.%s".format(),
            )
            logging.getLogger("tracing.span").setLevel(logging.WARNING)
            logging.getLogger("daft_io.stats").setLevel(logging.WARNING)
            logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING)

        ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging})
        daft.context.set_runner_ray()
    daft.set_execution_config(actor_udf_ready_timeout=600)
    daft.set_execution_config(min_cpu_per_task=0)
    
    samples = {
        "text": [
            """<!-- 这是 HTML 注释,不会在浏览器中显示 -->
    <!DOCTYPE html>
    <html>
    <head>
        <!-- 引入外部 CSS -->
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <!--
            多行注释示例:
            保持代码缩进一致(如 2/4 空格)。
            标签属性使用双引号。
        -->
        <div class="content" id="main-content">
            <p class="text">Hello World!</p>
        </div>
    </body>
    </html>""",
            None,
        ]
    }

    separator = "\n"
    strip = True
    ds = daft.from_pydict(samples)

    ds = ds.with_column(
        "cleaned_text",
        las_udf(
            CleanHtmlTag,
            construct_args={"separator": separator, "strip": strip},
        )(col("text")),
    )
    ds.show()

    # ╭──────────────────────────────────────────────┬──────────────╮
    # │ text                                         ┆ cleaned_text │
    # │ ---                                          ┆ ---          │
    # │ Utf8                                         ┆ Utf8         │
    # ╞══════════════════════════════════════════════╪══════════════╡
    # │ <!-- 这是 HTML 注释,不会在浏览器中显示 -->…      ┆ Hello World! │
    # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
    # │ None                                         ┆ None         │
    # ╰──────────────────────────────────────────────┴──────────────╯
最近更新时间:2026.01.08 19:14:22
这个页面对您有帮助吗?
有用
有用
无用
无用