HTML文档净化处理器 - 多结构解析与智能清理解决方案
<h1>-<h6>标签<script>/<style>等非文本标签BeautifulSoup4(html.parser)输入列名 | 说明 |
|---|---|
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 │ # ╰──────────────────────────────────────────────┴──────────────╯