【问题标题】:How to use a custom parser with Python's Beautiful Soup?如何在 Python 的 Beautiful Soup 中使用自定义解析器?
【发布时间】:2018-05-31 12:28:11
【问题描述】:

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

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>

【问题讨论】:

  • 您是否尝试使用 BS 和 angular ?你有问题吗?在示例中显示它。
  • @furas 我在帖子中添加了一个示例。

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


【解决方案1】:

您可以使用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

【讨论】:

    猜你喜欢
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    相关资源
    最近更新 更多