跳转到主要内容

清理和标准化HTML。

项目描述

PyPI Version Supported Python Versions Build Status Coverage report

清理和标准化HTML。保留嵌入内容(例如Twitter、Instagram等)

快速开始

安装

使用pip安装库

pip install clear-html

使用

使用lxml的示例用法

from lxml.html import fromstring
from clear_html import clean_node, cleaned_node_to_html

html="""
        <div style="color:blue" id="main_content">
            Some text to be
            <div>cleaned up!</div>
        </div>
     """
node = fromstring(html)
cleaned_node = clean_node(node)
cleaned_html = cleaned_node_to_html(cleaned_node)
print(cleaned_html)

使用Parsel的示例用法

from parsel import Selector
from clear_html import clean_node, cleaned_node_to_html

selector = Selector(text="""<html>
                            <body>
                                <h1>Hello!</h1>
                                <div style="color:blue" id="main_content">
                                    Some text to be
                                    <div>cleaned up!</div>
                                </div>
                            </body>
                            </html>""")
selector = selector.css("#main_content")
cleaned_node = clean_node(selector[0].root)
cleaned_html = cleaned_node_to_html(cleaned_node)
print(cleaned_html)

上述两种不同方法都会打印以下内容

<article>

<p>Some text to be</p>

<p>cleaned up!</p>

</article>

其他有趣的功能

  • cleaned_node_to_text:将清理后的节点转换为纯文本

  • formatted_text.clean_doc:低级方法,用于控制清理的更多方面

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码发行版

clear_html-0.4.1.tar.gz (23.9 kB 查看哈希值)

上传于

构建分发版

clear_html-0.4.1-py3-none-any.whl (24.7 kB 查看哈希值)

上传于 Python 3

支持