Skip to main content

Haruka Parser

A simple HTML Parser

Install

pip install haruka-parser

Usage

V2(推荐)

V2 是多引擎投票的抽取管线:trafilatura 与 readability 两路主内容启发式对段落投票, 只保留双引擎都命中的正文块;在此基础上叠加 math/code/table 的结构化转换与块级去重。

from haruka_parser.v2.extractors.auto_extractor import AutoExtractor

extractor = AutoExtractor()
result = extractor.extract(html, url="https://example.com/page.html")

print(result["content"])     # Markdown 正文
print(result["title"])       # 标题
print(result["time"])        # 发布时间 "2024-05-11 08:30:00"(无则 "")
print(result["time_stamp"])  # 对应 unix timestamp(字符串,无则 "")

返回字段:

字段 说明
content Markdown 格式正文
paragraphs 段落结构(含 unique_id、tag、dom meta),可回放
dom_attrs / links DOM 属性与页面链接(链接按 base_url 补全)
title / time / time_stamp 标题、发布时间及其 timestamp
base_url / encoding 最终基准 URL 与检测到的编码

常用参数:

extractor.extract(
    html,
    url="https://example.com/page.html",  # 用于链接补全、域名特判、标题回退
    separator="\n\n",                     # 段落分隔符
    table_format="github",                # github / grid / simple / html / html_raw / html_auto
    escape_dollars=False,                 # 非 LaTeX 上下文的 $ 转义
    use_ftfy=False,                       # ftfy 文本修复
    magic_html_heuristic=False,           # 引入 magic-html 作为第三路投票
    dedup=True,                           # 块级去重(页面内重复渲染的内容只留第一份)
)

clean 模式:已清洗的上游输入

当上游 HTML 已经清洗过(例如只有正文的 body 片段)时,用 mode="clean" 跳过所有会删正文 的内容启发式(traf/read/magic 投票、link_list 打标、dedup、clean_tree、wiki 特判、 交互/不可见/广告元素清理),只做 math/code/table 等结构化 DOM 处理和 paragraphs 转换。 标题/时间抽取照常执行;script/head/iframe 等非正文标签仍会剔除。

result = extractor.extract(cleaned_body_html, mode="clean", url="https://example.com/docs/")

V1(旧接口)

from haruka_parser.extract import extract_text

html = """<!DOCTYPE html>
<html>
<body>
<!-- Using MathML -->
<p>Using MathJax:</p>
<script type="math/tex; mode=display" id="MathJax-Element-1">{e}^{i\pi }=-1</script>
<!-- Using MathML -->
<p>Using MathML:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <msup>
    <mi>e</mi>
    <mrow>
      <mi>i</mi>
      <mi>&#x03C0;</mi>
    </mrow>
  </msup>
  <mo>=</mo>
  <mn>-1</mn>
</math>

<!-- Using AsciiMath -->
<p>Using AsciiMath:</p>
<script type="math/asciimath">
e^(i*pi) = -1
</script>

</body>
</html>"""

text, info = extract_text(html)
print(text)
print(info)

Configurations

from haruka_parser.extract import DEFAULT_CONFIG
DEFAULT_CONFIG = {
    "readability": False,
    "skip_large_links": False,
    "extract_latex": True,
    "extract_cnki_latex": False,
    "escape_dollars": True,
    "remove_buttons": True,
    "remove_edit_buttons": True,
    "remove_image_figures": True,
    "markdown_code": True,
    "markdown_headings": True,
    "remove_chinese": False,
    "boilerplate_config": {
        "enable": False,
        "ratio_threshold": 0.18,
        "absolute_threshold": 10,
        "end_threshold": 15,
    },
}

Parsing speed

10k Page:

method haruka-parser 0.5.2 haruku-parser 0.4.9 html2text inscriptis trafilatura
Speed 379.4s 391.6s 272.8s 114.7s 343.9s

Metadata

Release files for haruka-parser 1.3.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for haruka-parser 1.3.5
File Size Uploaded Provenance Actions
haruka_parser-1.3.5.tar.gz 34.4 MB Without provenance Details for haruka_parser-1.3.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for haruka-parser 1.3.5
File Interpreter ABI Platform Actions
haruka_parser-1.3.5-py3-none-any.whl Python 3 none any Details for haruka_parser-1.3.5-py3-none-any.whl

Total release size: 69.1 MB

Release files / haruka_parser-1.3.5.tar.gz

Download URL haruka_parser-1.3.5.tar.gz
Size 34.4 MB
Tags Source
SHA-256 checksum
How to use checksums
30b771881dd78a856708832798a7061abbb904b1a6cdeee3b98c262cc25405fa
BLAKE2b-256 checksum
How to use checksums
8ee5abfdcf5cce84d1f5296ccac64870d3f5de9bc8f309f6846753e1e1957b36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / haruka_parser-1.3.5-py3-none-any.whl

Download URL haruka_parser-1.3.5-py3-none-any.whl
Size 34.7 MB
Tags Python 3
SHA-256 checksum
How to use checksums
b3fd265b6ae4e8b4f2a32378ae421dd738ae642b2bcda772c0a221aa64f4aab1
BLAKE2b-256 checksum
How to use checksums
1f6e7f758f16a794887b1c6c26ae4bbf12e0bdcfa2f1ffa0be6bddaad3f3edd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for haruka-parser 1.3.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.3.5 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page