CommonCrawl_WARC文件内容提取
更新时间:2026-05-18
CommonCrawl WARC文件内容提取
简介
CommonCrawl WARC 文件内容提取算子,从 CommonCrawl 格式的 WARC 文件中批量提取正文文本,支持 trafilatura 和 justext 两种提取引擎。
功能描述
- 解析 WARC 格式文件,批量提取网页正文
- 支持 trafilatura(推荐)和 justext 两种正文提取引擎
- 支持 WARC 文件的本地路径和远程路径输入
- 支持二进制 WARC 数据输入
- 可通过 max_records 限制处理记录数
- 依赖 trafilatura(
pip install trafilatura)
算子参数
输入
| 输入 | 含义 |
|---|---|
| warc_col | WARC 文件路径或二进制数据数组,类型由 warc_src_type 决定 |
输出
| 输出 | 含义 |
|---|---|
| extracted_texts | 从 WARC 文件中提取的正文文本列表(list[string]),每个元素对应一条 WARC 记录的正文 |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| warc_src_type | str | (必填) | WARC 数据来源类型:warc_path(本地/远程文件路径)、warc_binary(二进制数据) |
| extractor_type | str | "trafilatura" | 正文提取引擎:trafilatura 或 justext |
| max_records | int 或 None | None | 每个 WARC 文件最多处理的记录数,None 表示处理全部 |
调用示例
Plain Text
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.text.commoncrawl_content_extractor import CommonCrawlContentExtractor
10
11if __name__ == "__main__":
12 if os.getenv("DAFT_RUNNER", "native") == "ray":
13 import ray
14 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
15 daft.set_runner_ray()
16 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
17
18 samples = {
19 "warc_path": [
20 "/tmp/CC-MAIN-2024-01.warc.gz",
21 "/tmp/CC-MAIN-2024-02.warc.gz",
22 ]
23 }
24 ds = daft.from_pydict(samples)
25 constructor_kwargs = {
26 "warc_src_type": "warc_path",
27 "extractor_type": "trafilatura",
28 "max_records": 1000,
29 }
30 ds = ds.with_column(
31 "texts",
32 aihc_udf(
33 CommonCrawlContentExtractor,
34 construct_args=constructor_kwargs,
35 num_cpus=1,
36 concurrency=4,
37 batch_size=4,
38 )(col("warc_path")),
39 )
40 ds.show()
评价此篇文章
