python - 如何将自定义解析器与 Python 的 Beautiful Soup 一起使用?

标签 python python-3.x parsing beautifulsoup html-parsing

我正在使用 Beautiful Soup 4解析和修改一组 HTML 文件。 HTML 文件是 Angular 模板,这意味着它们的标记在某种程度上与常规 HTML 文档中的标记不同(混合大小写属性、指令、输入/输出绑定(bind)等)。

Beautiful Soups documentation 中没有列出任何解析器(html.parser, lxml, html5lib) 完全符合我的需要。最接近它的是 Python 内置的 html.parser,但我不得不对其进行一些调整。

是否可以将自定义解析器类与 Beautiful Soup 一起使用?如果是,如何实现?

编辑: 下面是 Beautiful Soup 如何解析模板的示例。主要问题是结果中的所有属性都是小写的(*ngIf -> *ngif)并且指令(appAutoFocus、appKeepFocusInside 等)得到一个空字符串值(例如 appautofocus="")。

from bs4 import BeautifulSoup

test_html = """
<kendo-dialog *ngIf="dialogOpened" appAutoFocus appKeepFocusInside (close)="closeDialog()" width="1000">
   <kendo-dialog-titlebar>A title</kendo-dialog-titlebar>
   <div *ngIf="model.showValidationFailedMessage" class="alert alert-danger alert-dismissible">
      <button type="button" class="close" aria-label="Close" (click)="model.closeValidationAlert()"><span
         aria-hidden="true">&amp;times;</span></button>
      Validation failed
   </div>

   <kendo-tabstrip [keepTabContent]="true">
      <kendo-tabstrip-tab title="Tab title 1" [selected]="true">
         <ng-template kendoTabContent>
            <div>Tab content</div>
         </ng-template>
      </kendo-tabstrip-tab>
   </kendo-tabstrip>
</kendo-dialog>
"""

document = BeautifulSoup(test_html, "html.parser")
print(document.prettify())

结果:

<kendo-dialog (close)="closeDialog()" *ngif="dialogOpened" appautofocus="" appkeepfocusinside="" width="1000">
 <kendo-dialog-titlebar>
  A title
 </kendo-dialog-titlebar>
 <div *ngif="model.showValidationFailedMessage" class="alert alert-danger alert-dismissible">
  <button (click)="model.closeValidationAlert()" aria-label="Close" class="close" type="button">
   <span aria-hidden="true">
    &amp;times;
   </span>
  </button>
  Validation failed
 </div>
 <kendo-tabstrip [keeptabcontent]="true">
  <kendo-tabstrip-tab [selected]="true" title="Tab title 1">
   <ng-template kendotabcontent="">
    <div>
     Tab content
    </div>
   </ng-template>
  </kendo-tabstrip-tab>
 </kendo-tabstrip>
</kendo-dialog>

最佳答案

您可以使用 bs4.builder.register_treebuilders_from .内置构建器是 bs4.builder 中的模块包裹。例如,bs4.builder._lxml .

这是 register_treebuilders_from

def register_treebuilders_from(module):
    """Copy TreeBuilders from the given module into this module."""
    # I'm fairly sure this is not the best way to do this.
    this_module = sys.modules['bs4.builder']
    for name in module.__all__:
        obj = getattr(module, name)

        if issubclass(obj, TreeBuilder):
            setattr(this_module, name, obj)
            this_module.__all__.append(name)
            # Register the builder while we're at it.
            this_module.builder_registry.register(obj)

和包的根模块的尾部。

# Builders are registered in reverse order of priority, so that custom
# builder registrations will take precedence. In general, we want lxml
# to take precedence over html5lib, because it's faster. And we only
# want to use HTMLParser as a last result.
from . import _htmlparser
register_treebuilders_from(_htmlparser)
try:
    from . import _html5lib
    register_treebuilders_from(_html5lib)
except ImportError:
    # They don't have html5lib installed.
    pass
try:
    from . import _lxml
    register_treebuilders_from(_lxml)
except ImportError:
    # They don't have lxml installed.
    pass

因此,您需要创建一个带有 bs4.builder.TreeBuilder 子类的模块,并将其注册到模块的 __all__ 中。然后将模块传递给 register_treebuilders_from

关于python - 如何将自定义解析器与 Python 的 Beautiful Soup 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47863781/

相关文章:

python - 寻找一种避免合并两个数据帧的优雅解决方案

java - 是否可以通过Crawler4j检索网站内容?

sql - 在运行时将 SQL 转换为 Linq To Objects 表达式

python - 使用reduce在python中乘以成对元素

python - 对话流 : How to add parameter automatically when using the dialogflow python API

python - 查找数据框中每一列的平均值,按列分组,不包括一个值

c# - 如何正确解析 SQL CREATE TABLE 语句

python - Pickle 包含 'n' 2 元组值的列表

python - `arr_2 = arr`和 `arr_2 = arr.view()`和 `arr_ 2 = arr.copy()`有什么区别

python-3.x - 回归 : OAuth Invalid Scope (Google Hangouts - Hangups Library)